written 8.3 years ago by |
The Document Object Model (DOM) is an object-oriented representation of an HTML or XML document. The structure of an HTML and XML document is hierarchical, so the DOM structure resembles that of a tree. DOM provides an API to access and modify this tree of objects. The DOM API is specified in a language- independent manner by the W3C, and mappings are available for most programming languages. The DOM API is used in a number of situations, like on the server side in a Java program to perform some manipulation on an XML document. For the scope of this and the next chapter, we will look in more details at the DOM in the context of the browser and see how with DOM API you can dynamically modify the HTML or XHTML page.
In order to look at the structure of the DOM, consider this short, yet complete and valid XHTML page:
<?xml version=”1.0” encoding=”ISO-8859-1”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”https://www.w3.org/1999/xhtml”>
<head>
<title>DOM Example</title>
</head>
<body>
<h1>Page title</h1>
<p>
Some <i>very</i> unimportant text.
</p>
</body>
</html>
When displayed by a browser, it looks like Figure:
Levels of DOM:
The DOM has three levels of support; each level corresponds to subsequent recommendations by the W3C:
Level 1 was first released in 1998, when browsers were already providing a DOM-like API. It was released in part as an effort to define a common API that would be implemented by web browsers. Adoption has been slow, but today it is generally accepted that DOM level 1 is supported by all the mainstream browsers.
Level 2 was first released in 2000, and most of the API is supported by all the mainstream browsers, except Internet Explorer which implements a smaller but still significant subset of the specification. If you are developing an application targeting the browsers deployed today, using level 2 recommendations as a reference is your best bet.
Level 3 is still a work in progress. Starting with level 2, the DOM specification has been split into a number of documents: one specification for core components, one for HTML-specific aspects of the API, one dealing with events, and so on. Only a subset of those has been published by the W3C as recommendations, while others are still works in progress. Most browsers only support a minimal subset of level 3, and Internet Explorer does not support level 3 at all. For those reasons, at this point most web developers consider that it is still too early to use the DOM level 3.