w3c dom compatibility core
source: http://www.quirksmode.org/dom/w3c_core.html
BY Peter-Paul Koch
The Four Methods
First of all the Four Methods. The average W3C DOM script uses them all. The first two methods allow you to create element nodes and text nodes. Later you insert these newly created nodes into the document.
The second two methods are for finding elements in the page. You can either find a single one, identified by an id, or all tags of one type.
You must know these methods by heart.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
createElement() Create a new element |
Yes | Yes | Yes | Yes | Yes | Yes | ||
var x = document.createElement('P')
Create a new HTML element node
|
||||||||
|
createTextNode() Create a new text node |
Yes | Yes | Yes | Yes | Yes | Yes | ||
var x = document.createTextNode('text')
Create a text node with content |
||||||||
|
getElementById() Get the element with this ID Test page Lower case ‘d‘!! |
Almost | Yes | Yes | Yes | Yes | Yes | ||
var x = document.getElementById('test')
Take the element with If there is more than one element with
|
||||||||
|
getElements Get all tags of this type |
Incom |
Yes | Yes | Yes | Yes | Incom |
Incom |
|
var x = document.getElementsByTagName('P')
Make var x = y.getElementsByTagName('P')
Gets all paragraphs that are descendants of node
|
||||||||
Node information
These four properties give basic information about all nodes. What they return depends on the node type. They are read-only, except for nodeValue.
There are three basic node types: element nodes (HTML tags), attribute nodes and text nodes. I test these properties for all these three types and added a fourth node type: the document node (the root of all other nodes).
You must know these properties by heart.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
nodeName The name of the node |
Incom |
Yes | Yes | Almost | Almost | Yes | Almost | ||||||||||
x.nodeName The name of node
|
|||||||||||||||||
|
nodeType The type of the node |
Incom |
Yes | Yes | Almost | Almost | Yes | Almost | ||||||||||
x.nodeType The type of node
|
|||||||||||||||||
|
nodeValue The value of the node, if any. Read/write |
Incom |
Yes | Yes | Almost | Almost | Yes | Almost | ||||||||||
x.nodeValue Get the value of node x.nodeValue = 'Test' Set the value of node
|
|||||||||||||||||
|
tagName The tag name of an element node Test page Don’t use |
Almost | Yes | Yes | Yes | Yes | Yes | |||||||||||
x.tagName Get the tag name of node
My advice is not to use
|
|||||||||||||||||
The DOM tree
Five properties and two arrays for walking through the DOM tree. Using these properties, you can reach nodes that are close to the current node in the document structure.
In general you shouldn’t use too many of these properties. As soon as you’re doing something like
x.parentNode.firstChild.nextSibling.children[2]
your code is too complicated. Complex relationships between nodes can suddenly and unexpectedly change when you alter the document structure, and altering the document structure is the point of the W3C DOM. In general you should use only one or two of these properties per action.
You must know these properties by heart.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
childNodes[] An array with all child nodes of the node |
Yes | Yes | Yes | Yes | Yes | Almost | ||
x.childNodes[1] Get the second child node of node The
|
||||||||
|
children[] An array with all child element nodes of the node |
Almost | No | Yes | Yes | Yes | Yes | ||
x.children[1] Get the second element child node of node Where
|
||||||||
|
firstChild The first child node of the node |
Yes | Yes | Yes | Yes | Yes | Almost | ||
x.firstChild Get the first child node of node
|
||||||||
|
hasChildNodes() Check if the node has child nodes |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.hasChildNodes() Returns |
||||||||
|
lastChild The last child node of the node |
Yes | Yes | Yes | Yes | Yes | Almost | ||
x.lastChild Get the last child of node
|
||||||||
|
nextSibling The next sibling node of the node |
Yes | Yes | Yes | Yes | Yes | Almost | ||
x.nextSibling Get the next child of the parent of
|
||||||||
|
parentNode The parent node of the node |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.parentNode Get the parent node of |
||||||||
|
previousSibling The previous sibling node of the node |
Yes | Yes | Yes | Yes | Yes | Almost | ||
x.previousSibling Get the previous child of the parent of
|
||||||||
|
sourceIndex The index number of the node in the page source |
Yes | No | No | Yes | Incor |
Yes | ||
x.sourceIndex Get the
|
||||||||
Node manipulation
These five methods allow you to restructure the document. The average DOM script uses at least two of these methods.
The changes in the document structure are applied immediately, the whole DOM tree is altered. The browser, too, will immediately show the changes.
You must know these methods by heart.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
appendChild() Append a child node as the last node to an element |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.appendChild(y) Make node If you append a node that’s somewhere else in the document, it moves to the new position. |
||||||||
|
cloneNode() Clone a node |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x = y.cloneNode(true | false) Make node Later you insert the clone into the document. |
||||||||
|
insertBefore() Insert a node into the child nodes of an element |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.insertBefore(y,z) Insert node |
||||||||
|
removeChild() Remove a child node from an element |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.removeChild(y) Remove child |
||||||||
|
replaceChild() Replace a child node of an element by another child node |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.replaceChild(y,z) Replace node |
||||||||
Data
These methods are for manipulating text data. Note the difference between a text node and text data: the text node is a node and contains the data in its nodeValue. The methods below only work with this contained data.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
appendData() Append data to a text node |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.appendData(' some extra text')
Appends the string |
||||||||
|
data The content of a text node |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.data The content of Can also be set: x.data = 'The new text' |
||||||||
|
deleteData() Delete text from a text node |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.deleteData(4,3) Delete some data from |
||||||||
|
insertData() Insert text into a text node |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.insertData(4,' and now for some extra text ') Insert the string |
||||||||
|
replaceData() Replace text in a text node |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.replaceData(4,3,' and for some new text ') Replace three characters, beginning at the fifth one, of node |
||||||||
|
substringData() Take a substring of the text in the text node |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.substringData(4,3) Takes a substring of |
||||||||
Attributes
A bloody mess. Try influencing attributes in this order:
- Try getting or setting a specific property, like
x.idory.onclick. - If there is no specific property, use
getAttribute()orsetAttribute(). - If even that doesn’t work, try any other method or property in the table below. Most have horrible browser incompatibility patterns, though.
- Avoid
attributes[]. It’s worse than anything else.
In my view any method or property concerning attribute nodes should also work on the style attribute, event handlers and custom attributes. If not I judge the method or property incomplete.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
attributes[index] An array with the attributes of a node, accessed by index number Test page Do not use Use |
Alter |
Incor |
Yes | Yes | Yes | Yes | ||
x.attributes[1] This simple line may have several meanings:
This array consists of all defined attributes in all browsers save IE, where it consists of all attributes that can possibly be defined on the node (84 all in all). Do yourself a favour and don’t use the indexed
|
||||||||
|
attributes[key] An array with the attributes of a node, accessed by attribute name |
Incor |
Almost | Yes | Yes | Yes | Yes | No | |
x.attributes['align'] Get the align attribute object of node After years in the wilderness
|
||||||||
|
createAttribute() and setAttributeNode() Create a new attribute node and append it to an element node. |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
z = document.createAttribute('title');
z.value = 'Test title';
x.setAttributeNode(z)
This creates a title attribute with a value and sets it on node |
||||||||
|
getAttribute() Get the value of an attribute |
Almost | Yes | Yes | Yes | Yes | Yes | ||
x.getAttribute('align')
Gives the value of the align attribute of node
|
||||||||
|
getAttributeNode() Get an attribute node |
No | Almost | Yes | Yes | Yes | Yes | Yes | |
x.getAttributeNode('align')
Get the attribute object
|
||||||||
|
hasAttribute() Check if a node has a certain attribute |
No | Yes | Yes | Yes | Yes | Yes | ||
x.hasAttribute('align')
Returns |
||||||||
|
hasAttributes() Check if a node has attributes |
No | Yes | Yes | Yes | Yes | Yes | ||
x.hasAttributes() Returns |
||||||||
|
name The name of an attribute |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.name The name of attribute node |
||||||||
|
removeAttribute() Remove an attribute node |
Almost | Yes | Yes | Almost | Yes | Almost | ||
x.removeAttribute('align')
Remove the
|
||||||||
|
remove Remove an attribute node |
No | Minimal | Incom |
Yes | Yes | Almost | Minimal | Almost |
x.removeAttributeNode(x.attributes['align'])
x.removeAttributeNode(x.attributes[1])
x.removeAttributeNode(x.getAttributeNode('align'))
Removes the attribute node. There is little difference with
|
||||||||
|
setAttribute() Set the value of an attribute |
Incom |
Yes | Yes | Yes | Incom |
Yes | ||
x.setAttribute('align','right')
Set the align attribute of node
|
||||||||
|
setAttributeNode() |
See createAttribute() |
|||||||
|
value The value of an attribute |
No | Almost | Yes | Yes | Yes | Yes | Yes | |
x.value The value of attribute
|
||||||||
Miscellaneous
A lot of miscellaneous methods and properties that you’ll rarely need. I use only two of them in an actual script.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
contains() Check whether an element contains another element |
Yes | No | Yes | Yes | Yes | Yes | ||
x.contains(y) If node |
||||||||
|
createDocument() Create a new document |
No | Yes | Yes | Yes | Yes | Yes | ||
x = document.implementation.createDocument('','',null)
Creates a new document. The the Import XML script gives an example of what to do with such a document. |
||||||||
|
createDocument Create a document fragment |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x = document.createDocumentFragment(); x.[fill with nodes]; document.[somewhere].appendChild(x); Create a fragment, add a lot of nodes to it, and then insert it into the document. Note that the fragment itself is not inserted, only its child nodes. You may not move a node from the existing document to the document fragment. (Cloning is allowed, however.) |
||||||||
|
documentElement The HTML tag |
Yes | Yes | Yes | Yes | Yes | Yes | ||
document.documentElement Represents the root element of the XML document. In any HTML document, the |
||||||||
|
getElements Get elements by their name attribute |
Incor |
Yes | Yes | Incor |
Incom |
Incom |
||
var x = document.getElementsByName('test')
Create a nodeList with all elements that have On my test page the
|
||||||||
|
hasFeature() Check if an element has a certain feature. |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
document.implementation.hasFeature('XML','1.0')
Returns Note that this method reports the browser’s own assessment of its capabilities. There is no independent check. |
||||||||
|
item() Access an item in an array Test page Not necessary in JavaScript |
Yes | Yes | Yes | Yes | Yes | Yes | ||
document.getElementsByTagName('P').item(0)
The same as The You don’t need |
||||||||
|
normalize() Merge adjacent text nodes into one node |
No | Yes, but be careful | Yes | Yes | Yes | Crash | Yes | |
x.normalize() All child nodes of node This method used to crash Explorer 6. My last test doesn’t cause a crash, but I advise you to be careful.
|
||||||||
|
ownerDocument The document that ‘owns’ the element |
No | Yes | Yes | Yes | Yes | Yes | Yes | |
x.ownerDocument Refers to the document object that ‘owns’ node |
||||||||
|
splitText() Split a text node into two text nodes |
Yes | Yes | Yes | Yes | Yes | Yes | ||
x.splitText(5) Split the text node |
||||||||
Microsoft extensions
As usual Microsoft has extended the standard somewhat. Though sometimes its extensions are brilliant (innerHTML springs to mind), in the case of the DOM Core they aren’t.
Note the difference between W3C and Microsoft methods. The W3C methods are owned by the parent element of the node you want to adjust, the Microsoft methods by the node itself.
See also the key to my compatibility tables.
| Method or property | IE 5.5 | IE 6 | IE 7 | Firefox 2.0 | Safari 3.0 Win | Opera 9.5b | iCab 3.0 | Konqueror 3.5.7 |
|---|---|---|---|---|---|---|---|---|
|
applyElement() Something with nodes |
Something happens | No | No | No | No | No | ||
x.applyElement(y) IE does something when this line is executed, but I’m not sure exactly what is happening, nor whether it’s supposed to be happening. Try to find out for yourself by studying the singularly vague MSDN reference. |
||||||||
|
clearAttributes() Remove all attributes from a node |
Incom |
No | No | No | No | No | ||
x.clearAttributes() Remove all attributes from node
|
||||||||
|
merge Copy all attributes of one node to another node |
Yes | No | No | No | No | No | ||
x.mergeAttributes(y) Copy all of node |
||||||||
|
removeNode() Remove a node |
Yes | No | No | Yes | No | No | ||
x.removeNode(true | false) Remove node |
||||||||
|
replaceNode() Replace a node by another node |
Yes | No | No | No | No | No | ||
x.replaceNode(y) Replace node |
||||||||
|
swapNode() Swap two nodes |
Yes | No | No | No | No | No | ||
x.swapNode(y) Put node |
||||||||



