The Document Object Model (DOM) is a standard that governs programmatic access to document structures in XML and HTML documents. The DOM standard itself is independent of the programming language. This presentation only covers the JavaScript implementation of DOM.
The DOM structure is a Composite structure representing the contents of a web page. It consists of the following types of elements:
For JavaScript running in a browser, the displayed document
can be accessed through the document
global variable.
This is not itself part of the Composite structure (it has type
HTMLDocument), but it does provide access to the structure through its
variable document.documentElement
.
This is an element of type HTMLHtmlElement, representing the
<html> tag and its contents.
Usually, document elements are accessed by invoking search methods that
are provided by the document
variable.
Element methods give JavaScript programmers the capability of adding and
removing children and modifying their attributes such as their HTML
class and their ids.
The textual content of a Text element is easily modified by altering one
of its attributes.
Comment elements are rarely accessed since they do not affect the
display of a web page.
The attributes of elements in a document hierarchy can often be modified by JavaScript code. Assigning a new value to an attribute works like using a setter method in Java - it can have side effects. All modifiable attributes that affect the display of a document have updating the display as a side effect.
In this presentation, all attributes are modifiable except those that are described as read-only.
DOM standards describe interfaces for various types of objects. The description is given assuming a class-based language, but it can be bound to any object-oriented language, including classless languages. All that DOM standards require is that the binding behaves as if the various DOM classes inherit from other classes. That is, objects of given DOM type have all attributes and methods of its parent classes.
When you read documentation about DOM and its JavaScript bindings, it is useful to know a little bit about the implied DOM class hierarchy because you may have to search up the inheritance ancestry to find the attributes and methods that an object has. This search has already been done for the attributes and methods described in this presentation, but it only scratches the surface. More complete documentation for a DOM class usually only describes attributes and methods added by the class.
For the DOM classes described in this presentation, the Node hierarchy is as follows.
For the DOM classes described in this presentation, the Document hierarchy is simple.
document
Variable
The document
global variable has type Document.
It has the following useful attribute:
document.documentElement
- the root of the document
hierarchy: the <html> tag and its contents.
The document
variable is usually a HTMLDocument.
Then it has the following additional attribute:
document.body
- the body of the document: the
<body> tag and its contents.
Any Document object has the following useful methods:
id
attribute is id.
The entire document hierarchy is searched.
All of the elements in the document hierarchy have the following useful attribute:
All of the elements in the document hierarchy have the following useful methods:
Nodes are not created by invoking constructors. They are created by invoking the Document methods createTextNode() and createElement().
All of the HTML tag elements have the following useful attributes:
The first two attributes are often specified directly in HTML as in the following example:
<div id="theDiv" class="theClass"> … </div>
Other attributes specified directly in HTML are also accessible as
element attributes in JavaScript.
Usually the attribute names are the same, but a few, such as
class
, are modified to avoid JavaScript keywords.
The HTML tag elements have no frequently used methods other than those shared by all elements.
Text nodes in the document hierarchy have the following useful attribute:
The data
attribute of a text node can be manipulated like
any other JavaScript string.
You do not need to (and should not in most cases) use HTML character
entities such as "<", ">", and "&".
Just use "<", ">", and "&".
David Flanagan, "JavaScript: The Definitive Guide", 6th Edition, O'Reilly, 2011.