Node | NN 6 IE 5 DOM 1 |
The Node object is an abstract representation in the W3C DOM of the fundamental content building block in a document. All pieces of content that you can address in the W3C DOM model are nodes: unnamed contiguous strings of text between tags, tagged elements, name/value attribute pairs, special-purpose elements such as comments, DOCTYPE declarations, and even the document, itself, to name several. |
|
A Node object has a large set of properties and methods, most of which concern a node's relationships to surrounding nodes. The objects in a document that scripts read and control are defined as descendants of the basic Node object; this means that the most common content-bearing objects that DHTML scripts work withHTML elements, text nodes, and element attributesall share this set of properties and methods to start. Then, as needed for their powers as HTML elements, they accrue additional properties and/or methods that give them their special powers. |
|
While the nodeness of the W3C DOM codifies the inheritance relationships among different pieces of a document's content, the model presents a conceptual framework and granularity that at times seems tedious compared to the shortcut HTMLness of both the first-generation DOM and the Microsoft DOM. But the ultimate goal is to provide a single model that works for both XML and HTML documents (in either their pure HTML or XML-ized versions). |
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE | NN 6 IE n/a DOM 1 | |||||||||||||||||||||||||
Read-only | ||||||||||||||||||||||||||
This set of constants belongs to the root Node object of the W3C DOM, and is therefore inherited by all document-level nodes and elements. Each property corresponds to an integer value associated with the nodeType property of every DOM node. You can use these properties as a more plain-language way to indicate the node type your script is looking for in comparisons or similar operations. |
||||||||||||||||||||||||||
Example | ||||||||||||||||||||||||||
if (myObject.nodeType == document.ELEMENT_NODE) { // process as an element here } |
||||||||||||||||||||||||||
Value | ||||||||||||||||||||||||||
Integer corresponding to DOM node type as follows. |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Default | ||||||||||||||||||||||||||
Constant values (above). |
addEventListener( ) | NN 6 IE n/a DOM 2 |
addEventListener("eventType", listenerFunction, useCapture) | |
Binds an event handler function to the current node so that the function executes when an event of a particular type arrives at the node either as event target or during event propagation. Note that W3C DOM events propagate through text nodes, as well as element nodes. The node listens for the event type either during event capture or event bubbling propagation, depending upon the setting of the Boolean third parameter. You may invoke this method multiple times for the same node but with different parameter values to assign as many event handling behaviors as you like, but only one listener function may be invoked for the same event and propagation type. If the event listener is added on a temporary basis, it may be removed via the removeEventListener( ) method. |
|
Parameters | |
|
|
Returned Value | |
None. |
appendChild( ) | NN 6 IE 5 DOM 1 |
appendChild(nodeObject) | |
Inserts a new node after the end of the last child node of the current node object. The current node object must be capable of containing child nodes, otherwise the method throws an exception. This method is the most common way to append a dynamically created element, text node, or document fragment to an existing element, such as a script might do when assembling a chunk of new content for a document. But if the node reference passed as a parameter with the appendChild( ) method points to an existing node in the document tree, that node is first removed from the tree, and then appended to the end of the list of child nodes in the current object. This provides a shortcut way to move a node from one location to the end of a container. |
|
Appending one text node as a sibling to an existing text node does not join the two text nodes together. To combine all sibling text nodes into one large text node, invoke the parent's normalize( ) method. |
|
Parameters | |
|
|
Returned Value | |
Reference to the appended node. |
attributes[ ] | NN 6 IE 5 DOM 1 |
Read-only | |
Returns a named node map object, which resembles an array (collection) of attribute objects (W3C DOM type Attr), but also has some methods of its own to facilitate accessing a member of this array. IE's attributes array contains entries for all attributes of the element's internal DTD, plus any custom (expando) attributes explicitly set in the HTML source code in IE 6. Scripted changes to the element's attributes or their values are not reflected in this array. |
|
For Netscape 6, the attributes array contains entries only for those attributes explicitly defined in the HTML source code for the element, including custom attributes. Scripted changes to attributes (additions or deletions) or their values are reflected in the attribute objects referenced by the attributes array. |
|
In lieu of the named node map object methods, you may access individual attribute objects via standard JavaScript array syntax. By and large, however, it is far more convenient to access HTML element attribute values for scripting purposes either via their reflection as element object properties or via the element's getAttribute( ) and setAttribute( ) methods. For W3C DOM details (which are useful for XML document parsing), see the Attr and NamedNodeMap objects for properties and methods of these objects. |
|
Example | |
var ldAttr = document.getElementById("myImg").attributes.getNamedItem("longdesc"); |
|
Value | |
Array (collection) of attribute object references in source code (Netscape 6), alphabetical-by-name (IE/Mac), or haphazard (IE/Windows) order. |
|
Default | |
Current element's model. |
childNodes[ ] | NN 6 IE 5 DOM 1 |
Read-only | |
This is a property of the W3C DOM Node object that consists of an array of references to all child nodes (a node list) in the next deeper level of the node hierarchy (whether part of the document node tree or free-standing document fragments not yet inserted into the document tree). To reach more deeply nested nodes, you must access the childNodes array of each child node of the current node. A vital property for walking through a node tree. See the NodeList object for the properties and methods of this kind of array. |
|
Example | |
for (var i = 0; i < nodeRef.childNodes.length; i++) { if (nodeRef.childNodes[i].nodeType == document.ELEMENT_NODE) { // operate on an element } } |
|
Value | |
Array of node object references. |
|
Default | |
Array of length zero. |
firstChild, lastChild | NN 6 IE 5 DOM 1 |
Read-only | |
Return a reference to the first or last child node (respectively) of the current element node. Most commonly, these child nodes are text nodes nested inside an element. For a simple element containing only one text node, both properties return a reference to the same text node. More complex constructions, such as tr elements, can have other element nodes (td elements) as their child nodes, but some browsers may turn source code carriage returns between elements into text nodes. Therefore, it's a good idea to validate the type of node returned by either property before acting on it. |
|
Example | |
if (document.getElementById("elementID").firstChild.nodeType == 3) {
// process as a text node
}
|
|
Value | |
Node object (including text node, HTML element node, etc.) reference. |
|
Default | |
null |
hasAttribute( ) | NN 6 IE n/a DOM 1 |
hasAttribute("attributeName") | |
Returns a Boolean value true if the current element has an attribute whose name matches the method parameter. |
|
Parameters | |
|
|
Returned Value | |
Boolean value: true | false. |
hasChildNodes( ) | NN 6 IE 5 DOM 1 |
Returns a Boolean value true if the current node contains one or more child nodes. |
|
Parameters | |
None. |
|
Returned Value | |
Boolean value: true | false. |
insertBefore( ) | NN 6 IE 5 DOM 1 |
insertBefore(newChildNode, referenceChildNodeOrNull) | |
Inserts a node as a child of the current node (usually the current node is an element) before one of the other child nodes of the current node. The new child can be a reference to an existing node in the document tree (in which case it is removed from its original position when this method is invoked). The child node may also be created anew as any valid DOM node type, including a document fragment (which may hold HTML tags) or Attr (the latter implemented for Netscape 6 and IE 6). |
|
The second parameter allows you to specify a reference point among existing child nodes, in front of which the new child node is inserted. Alternatively, if you specify null as the second parameter (or omit the parameter in IE), the new node is inserted as the last child of the current nodethe same result as the appendChild( ) method. |
|
Parameters | |
|
|
Returned Value | |
Reference to the inserted node object. |
isSupported( ) | NN 6 IE n/a DOM 2 |
isSupported("feature", "version") | |
Returns a Boolean true if the current node supports (i.e., conforms to the required specifications of) a stated W3C DOM module and version. While the document.implementation object's hasFeature( ) method performs the same test, it does so on the entire browser application. The isSupported( ) method performs the test on an individual node, allowing you to verify feature support for the current node type. Parameter values for isSupported( ) are the same as for document.implementation.hasFeature( ). |
|
It is up to the browser maker to validate that the DOM implemented in the browser conforms with each module before allowing the browser to return true for the module. That doesn't necessarily mean that the implementation is bug-free or consistent with other implementations. Caveat scriptor. |
|
In theory, you could use this method to verify module support prior to accessing a property or invoking a method, as in the following fragment that assumes myElem is a reference to an element node: if (myElem.isSupported("CSS", "2.0")) { myElem.style.color = "green"; } |
|
In practice, object detection is a better solution because W3C DOM support reporting facilities are not widely implemented yet and are certainly not backward compatible. |
|
Parameters | |
|
|
Returned Value | |
Boolean value: true | false. |
localName, namespaceURI, prefix | NN 6 IE n/a DOM 2 |
Read-only | |
These three properties apply primarily to XML document elements with tags that are defined with the help of XML namespaces. A simplified example of such a document follows: <?xml version="1.0" encoding="ISO-8859-1"> <results xmlns:libBook="http://catalog.umv.edu/schema"> <libBook:title libBook:rareBooks="true">De Principia</libBook:title> </results>? |
|
The properties reveal details about the element's naming characteristics. A localName is the equivalent of the nodeName property of the element, that is, the tag name within the scope of the entire document, even if the tag name is reused by another element originating from another namespace. The prefix, however, links the element with a prefix name that is normally defined with an xmlns attribute of a container in the XML document. This helps your script identify the namespace to which the element is associated. A further binding is revealed through the namespaceURI property, which returns the URI string assigned to the xmlns attribute of a container element. Although all three properties belong to the Node object, their values are null (or, rather, should be null, but in Netscape 6 are empty strings) for node types other than element and attribute nodes. |
|
Example | |
var allTitles = document.getElementsByTagName("title"); for (var i = 0; i < allTitles.length; i++) { if (allTitles[i].prefix == "libBook" && allTitles[i].namespaceURI.indexOf("catalog.umv.edu") != -1) { // process title elements from the desired namespace here } } |
|
Value | |
Strings. |
|
Default | |
For localName, the element's tag name. For others, an empty string. |
lastChild | |
See firstChild. |
nextSibling, previousSibling | NN 6 IE 5 DOM 1 |
Read-only | |
Return a reference to the next or previous node (respectively) in the document tree at the same nested level as the current node. If there is no node in the position indicated by the property name, the property returns null. For a lone text node inside an element node, both properties return null. Node sequence is determined intially by source code order, but script changes to the document tree are reflected in the nodes returned by these properties. |
|
Example | |
var nextNode = document.getElementById("elementID").nextSibling;
|
|
Value | |
Node object (including text node, HTML element node, etc.) reference. |
|
Default | |
null |
nodeName | NN 6 IE 5 DOM 1 |
Read-only | |
Returns a string that identifies the name of the node as influenced by the node type. For element and attribute node types, the property returns the tag name and attribute name, respectively. For many other kinds of nodes, which have no inherent label associated with them, the nodeName property returns a fixed string indicating the node type, such as #text for a text node and #document for the root document node. For elements, the property returns the same string value as the element object's tagName property. Note that browsers through IE 6 and Netscape 7 return element tag strings in all uppercase, regardless of source code style or DOCTYPE specification. |
|
Example | |
if (document.getElementById("elementID").nextSibling.nodeName == "#text") {
// process as a text node
}
|
|
Value | |
Fixed string for #cdata-section, #document, #document-fragment, and #text nodes; variable string for attribute, element, and other node types. |
|
Default | |
Node-specific. |
nodeType | NN 6 IE 5 DOM 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Read-only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns an integer that corresponds to a node type as specified in the W3C DOM. This is the preferred property to use to test a node object for its type (rather than the nodeName property values). Every node type has a value, but not all browsers that support the nodeType property support all node types as objects. The integer values have corresponding constants associated with them, which you can use to make more verbose, but easier-to-read script comparisons for node type processing (see the ATTRIBUTE_NODE property earlier in this chapter). Note that there is no way to distinguish element types (e.g., root Element node versus an HTMLElement node) via the nodeType property. Also note that IE 6 in Windows erroneously reports a DOCTYPE element as a comment node type. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (document.getElementById("elementID").firstChild.nodeType == 1) {
// process as an element
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Integer values according to the following table. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Node-specific. |
normalize( ) | NN 6 IE n/a DOM 2 |
Collapses all sibling text nodes of the current (element) node into a single text node. Invoking this method may be needed after inserting or removing child nodes of an element, if your node walking (traversal) scripts expect contiguous text to be contained by a single text node. The W3C DOM considers a document tree to be normal only if a text node has no other text nodes as siblings. |
|
Parameters | |
None. |
|
Returned Value | |
None. |
ownerDocument | NN 6 IE 5(Mac)/6(Win) DOM 1 |
Read-only | |
Returns a reference to the document object that contains the current node. Potentially helpful for functions that act on object references retrieved from event properties or passed as ID strings. The corresponding IE property is document. |
|
Example | |
var currDoc = document.getElementById("elementID").ownerDocument;
|
|
Value | |
document object reference. |
|
Default | |
The current document object. |
parentNode | NN 6 IE 5 DOM 1 |
Read-only | |
Returns a reference to the next outermost node (usually an element) that acts as a container to the current node in the document tree. The relationship between the current node and its parent is purely structural, and is not concerned with positioning context. A parent node is one that completely encases the current nodenot to be confused with sibling nodes, which, at best, reside on just one side of the current node. You can use the same cascading tricks as shown for the IE parentElement property, but it is hazardous to completely equate results from the element-centric IE-only properties with results from the W3C DOM node-centric properties (even though recent IE versions support both views of the world). |
|
Example | |
if (document.getElementById("elementID").parentNode.nodeType == 1) { document.getElementById("elementID").parentNode.style.fontSize = "14pt"; } |
|
Value | |
Element object reference. |
|
Default | |
Node-specific. |
removeChild( ) | NN 6 IE 5 DOM 1 |
removeChild(childNodeReference) | |
Removes a child node from the current element. The parameter must be a reference to an existing child node nested inside the current element. Once removed, the child node is no longer part of the document tree, but is still preserved in memory. The method returns a reference to the removed node so that you may modify it and place it elsewhere in the document tree. Note that you can command one node to remove one of its children, but you cannot command a node to remove itself (but see removeNode( ) for IE). |
|
Parameters | |
|
|
Returned Value | |
A reference to the removed node. |
swapNode( ) | NN n/a IE 5(Win) DOM n/a |
swapNode(otherNodeObject) | |
Exchanges the current node (in the document tree) with a different node passed as a parameter. The other node object can be created anew, or it can be a reference to a node elsewhere in the document tree. In the latter case, the result is the same as a bi-directional exchange, where the two nodes essentially change places. If the two nodes are of different node types or element display types (e.g., an inline versus a block-level element), the rendering of the document may be affected significantly. |
|
Parameters | |
|
|
Returned Value | |
Reference to the node from which the method is invoked (i.e., the current node). |