This page notes the new methods and properties that Version 5 browsers should support and the (in)compatibilities of Netscape 6 and Explorer 5, who (surprise!) don't support everything as well as theory says they should.
The table below is based on Explorer 5.0, Netscape 6.01, Opera 5.02 Windows/5.0B1 Mac and Konqueror 2.1 beta.
I haven't updated this page in a long time. Meanwhile I've created a
DOM Test Suite
but I haven't yet written the descriptions of the various methods and properties.
I've fallen behind in DOM research. If you want to keep in touch with the cutting edge, become
a member of the W3C DOM mailing list I founded.
I wrote an introduction to the Level 1 DOM so that you understand what nodes are and why you need them. On this page, first a large table of methods and properties and then four cases of trying to make generated events work (none cross-browser).
I tested each method and property by trying to use it in a live situation. I tested each method and property separately, so it's possbile that certain combinations won't work properly. I'll solve these problems on a case by case basis.
Remarks:
If you know of a method or property not listed here, please mail me.
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
---|---|---|---|---|
appendChild() | Supported | Supported | Supported | Not supported |
x.appendChild(y) Make node y the last child of node x. If you append a node that's somewhere else in the document, it moves to the new position. |
||||
appendData() | Supported only on Mac IE6! | Supported | Supported | Not supported |
x.appendData(' some extra text') Appends the string some extra text to x, which must be a text node. | ||||
applyElement() | Supported only on Windows | Not supported | Not supported | Not supported |
x.applyElement(y) Make node x a child of node y Even in Explorer 5 on Windows, this method only applies the actual node, not its content (text, for instance). Not recommended. |
||||
attributes[] Warning: Severe browser incompatibilities |
x.attributes[1] The value of the second possible attribute of node x (ALIGN). The array contains all possible attributes for node x, ordered alphabetically (not in Explorer 6). x.attributes['align'] is allowed. Self-defined attributes not allowed. If you want to know if an attribute is defined, use x.attributes[i].specified. |
x.attributes[1] The second actual attribute of node x. This is an object, not a value. The array contains all attributes that are in the HTML, in the order they appear in the source code. x.attributes['align'] not allowed. Also supports self-defined attributes (like BLAH="stuff"). |
Not supported | |
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
childNodes[] | Supported | Supported | Supported | Not supported |
x.childNodes[1] The second child node of node x. |
||||
className | Supported | Supported | Supported | Supported only on Mac |
x.className The value of attribute CLASS of node x. If the node doesn't have a class, it is empty. Can also be set: x.className="name" |
||||
clearAttributes() | Supported only on Windows | Not supported | Not supported | Not supported |
x.clearAttributes() Remove all attributes from node x |
||||
cloneNode() | Supported | Supported | Not supported | Not supported |
x = y.cloneNode(true | false) Make node x a copy of node y. If the argument is true, the entire tree below y is copied, if it's false only the root node y is copied. |
||||
contains() | Supported | Not supported | Supported | Supported |
x = y.contains(document.getElementById('test')) If the node y has as a descendant the node with ID=test, x becomes true, else x is false. |
||||
createAttribute() Warning: Useless in practice. |
Supported only on Mac IE6! | Supported | Untestable | Not supported |
x = document.createAttribute('align','right') Create a new attribute align with value right and temporarily place it in node x. The problem is that I have found no way of actually inserting the created attribute into the document. Neither appendChild() nor setAttribute() works and you can only use this method to create an attribute in the document, not in a deeper node. So the practical use of this method is effectively nil. |
||||
createElement() | Supported. ('<P>') is also allowed. | Supported | Supported | Not supported |
x = document.createElement('P') Create a new HTML element <P> and temporarily place it in node x. This node is later inserted into the document. |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
createTextNode() | Supported | Supported | Supported | Not supported |
x = document.createTextNode('text') Create a text node with content text and temporarily place it in node x This node is later inserted into the document. |
||||
data | Supported | Supported | Supported | Untestable |
x.data The content of x, which must be a text node. Equivalent to x.nodeValue . Can also be set: x.data = 'The new text'. | ||||
deleteData() | Supported only on Mac IE6! | Supported | Supported | Untestable |
x.deleteData(4,3) Delete some data from x, which must be a text node, starting at the fifth character and deleting three characters. | ||||
documentElement | Supported | Supported | Supported | Not supported |
x = document.documentElement.innerHTML In a way document.documentElement is the successor of the IE-specific DOM document.all. Developers will be thrilled that it contains good old innerHTML, also in Netscape 6. It contains a lot more interesting stuff, including most of the things that were included in document.all and it is also the node representing the root HTML tag of a document. I'll write a separate discussion of documentElement later on. | ||||
doctype | Supported only on Mac | Supported | Supported | Not supported |
document.doctype.name document.doctype gives access to the Doctype declaration at the start of the document. Example: <!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> The example above gives xhtml in Netscape 6 and xhtml plus the entire doctype tag in Explorer 5 on Mac. document.doctype als has the properties entities and notation to delve further into the arcana of DTD's. | ||||
firstChild | Supported | Supported | Supported | Not supported |
x.firstChild The first child of node x. |
||||
getAttribute() | Supported | Supported | Supported | Supported |
x = y.getAttribute('align') Give x the value of the attribute align of node y. In Opera the value of 'align' is always a number, but other attributes work fine. |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
getAttributeNode() | Supported only on Mac IE6! | Supported | Supported | Not supported |
x.getAttributeNode('onmouseover') Get the attribute onmouseover of node x. This is an object, not a value. If the attribute doesn't exist, it is empty. Through document.body.getAttributeNode() you can access the attributes of the <BODY> tag, but it's not entirely clear to me why we need something separate for that. |
||||
getElementById() | Supported | Supported | Supported | Supported |
x = document.getElementById('test') Take the element with ID="test" (wherever it is in the document) and put it in x. Now you can use all methods and properties on x and the element with ID="test" will change accordingly. This is the equivalent of document.layers and document.all in the Version 4 browsers and it is necessary to make your DHTML work. |
||||
getElementsByName() Warning: Browser incompatibilities |
Supported Explorer searches only for NAMEs within IMG, A, APPLET, OBJECT and all form-related tags. ID is the same as NAME. |
Supported Netscape searches for all NAMEs within all tags, even self-defined ones. ID is not a NAME. |
Supported Self-defined tags do not count, but all HTML tags do. ID is not a NAME. |
Supported Works only on a limited number of HTML elements. Only IDs count, not NAMEs. |
x = document.getElementsByName('nomen') Make x into an array of all elements with NAME="nomen" in the document, so x[1] is the second of those. |
||||
getElementsByTagName() | Supported | Supported | Supported | Supported only on Mac |
x = y.getElementsByTagName('P') Make x into an array of all P's that are children of node y, so x[1] is the second P etc. Using this method for self-defined tags (like <MYTAG>) is also allowed in Netscape and Explorer. document.getElementsByTagName('*') Returns a list of all tags in a document. Doesn't work in Explorer 5 on Windows. |
||||
getNamedItem() | Supported | Supported | Supported | Untestable |
x = y.attributes.getNamedItem('class').nodeValue Makes x the value of the CLASS attribute of node y. This method only works on namedNodeMaps, which you can envision as a kind of Perl hashes. Until now I only found attributes[] as an example of a namedNodeMap. |
||||
hasAttributes() | Not supported | Supported | Not supported | Not supported |
x = y.hasAttributes() Determine whether the node y has attributes. If yes x becomes true, if no x becomes false. |
||||
hasChildNodes() | Supported | Supported | Supported | Not supported |
x = y.hasChildNodes() Determine whether the node y has child nodes. If yes x becomes true, if no x becomes false. |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
id | Supported | Supported | Supported | Supported |
x.id The ID of node x, which must be a tag. If no ID is specified in the HTML, it is empty. Can also be set: x.id = 'newid' |
||||
implementation | Supported only on Mac IE6! | Supported | Supported | Not supported |
In itself implementation does nothing, it's only interesting for its two methods: createDocument() and hasFeatures(). | ||||
implementation. createDocument() | Not supported | Supported | Not supported | Not supported |
x = document.implementation.createDocument('','',null) See the Import XML page for a practical example. |
||||
implementation. hasFeature() | Supported only on Mac | Supported | Supported | Not supported |
x = document.implementation.hasFeature('XML','1.0') In the Level1 DOM, the implementation property is only interesting for its single method: hasFeature(). This method can be used to find out if a browser supports XML or HTML. The example above checks if the browser supports XML 1.0 . Although the W3C DOM Specification states that the '1.0' bit can be left out, Netscape 6 throws an error if you do. So Explorer 5 on Mac is the only browser to correctly support this method. |
||||
innerHTML | Supported | Supported | Crash; untestable | Not supported |
x.innerHTML The HTML contained by node x. Can also be set: x.innerHTML = 'The <B>new</B> text' |
||||
innerText | Supported | Not supported | Buggy | Not supported |
x.innerText The text contained by node x. If there are HTML tags in the node, these are ignored. Can also be set: x.innerText = 'The new text' |
||||
insertBefore() | Supported | Supported | Supported | Not supported |
x.insertBefore(y,z) Insert node y as a child of node x just before node z (which thus becomes y's nextSibling.) |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
insertData() | Supported only on Mac IE6! | Supported | Supported | Untestable |
x.insertData(4,' and now for some extra text ') Insert the string and now for some extra text after the fourth character into x, which must be a text node. | ||||
item() | Supported | Supported | Supported | Supported only on Mac |
x.getElementsByTagName('P'). The first element in the array created by x.getElementsByTagName('P'). It is completely equivalent to x.getElementsByTagName('P')[0] |
||||
lastChild | Supported | Supported | Supported | Not supported |
x.lastChild The last child of node x. |
||||
mergeAttributes() | Supported only on Windows | Not supported | Not supported | Not supported |
x.mergeAttributes(y) Copy all of node y's attributes to node x. |
||||
name | Supported only on Mac IE6! | Supported | Supported | Untestable |
x.name The name of x, which must be an attribute. |
||||
namedItem | Supported only on Mac | Supported | Not supported | Supported only on Mac |
x.getElementsByTagName('P'). Search for an element with ID testp in the array generated by document.getElementsByTagName('P'). Works only on such arrays. |
||||
nextSibling | Supported | Supported | Supported | Not supported |
x.nextSibling The next child of the parent of x. |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
nodeName | Supported | Supported | Supported | Not supported |
x.nodeName The name of node x. If x is text, it is #text, else it is the HTML tag or the name of the attribute. |
||||
nodeType | Supported | Supported | Supported | Not supported |
x.nodeType is 1 for a tag is 2 for an attribute is 3 for text |
||||
nodeValue | Supported | Supported | Supported | Untestable |
x.nodeValue The value of node x, which must be a text node or an attribute. You can also set the nodeValue: x.nodeValue = 'new value' |
||||
normalize() | Supported only on Mac Basic support in IE6, crashed on one test | Supported | Supported | Not supported |
x.normalize() All child nodes of node x that are text nodes and have as siblings other text nodes, are merged with those. This is in fact the reverse of splitText: text nodes that were split, come together again. |
||||
offsetParent | Supported | Supported | Supported | Supported |
x.offsetParent The parent node of x. Completely equivalent to parentNode. |
||||
parentNode | Supported | Supported | Supported | Supported |
x.parentNode The parent node of x. |
||||
previousSibling | Supported | Supported | Supported | Not supported |
x.previousSibling The previous child of the parent of x. |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
removeAttribute() Warning: Slightly buggy |
Supported | Supported | Supported | Not supported |
x.removeAttribute('align') Remove the align attribute of node x. Each browser removed the border of a table, but only Netscape 6 and Explorer 5 on Windows removed the ALIGN of a P. So the implementation is still a bit buggy. |
||||
removeAttributeNode() Warning: Vague implementation |
Basic support on Mac | Supported | Basic support | Not supported |
The main problem is naming the attribute you want to remove. x.removeAttributeNode(x.getAttributeNode('border')) Remove the border attribute of node x, which is a table. Done only by Netscape 6. x.removeAttributeNode(x.attributes[1]) Remove the second attribute of node x, which in this case is ALIGN. Done only by Netscape 6. This way of using removeAttributeNode() is impossible in Explorer, due to the strange implementation of attributes[]. |
||||
removeChild() | Supported | Supported | Supported | Not supported |
x.removeChild(y) Remove child y of node x. |
||||
removeNode() | Supported only on Windows | Not supported | Not supported | Not supported |
x.removeNode(true | false) Remove node x from the document. If you add true its children are also removed. |
||||
replaceChild() | Supported | Supported | Supported | Not supported |
x.replaceChild(y,z) Replace node z, a child of node x, by node y. |
||||
replaceData() | Supported buggily on Mac IE6! | Supported | Supported | Untestable |
x.replaceData(4,3,' and for some new text ') Replace three characters, beginning at the fifth one, of node x, which must be a text node, by the string and for some new text. In Explorer 5 on Mac the three characters are replaced by the first three characters of the new string, while the other characters of the new string are ignored. | ||||
replaceNode() |
Supported only on Windows | Not supported | Not supported | Not supported |
x.replaceNode(y) Replace node x by node y. |
||||
Method or property | Explorer 5.0 | Netscape 6.01 | Konqueror 2.1 | Opera 5 |
setAttribute() | Supported | Supported | Supported | Basic support |
x.setAttribute('align','left') Set the attribute align of node x to left. The name and value are both strings. Explorer 5 special effects: When you set the attribute style, the style of the element doesn't change. Attribute name is not allowed. Attribute names must be lower case. When you try to set the type attribute of an INPUT, Explorer 5 Windows used to give errors, but not any more, now it's Explorer 5 Mac that gives an error message. Strange... |
||||
setAttributeNode() | Supported only on Mac IE6! | Supported | Supported | Not supported |
Don't get this to work, Netscape returns an obscure error, though it does support the method. | ||||
specified | Supported | Supported | Supported | Untestable |
x.specified Is true when the attribute x has a value, false when it hasn't. |
||||
splitText() | Supported only on Mac IE6! | Supported | Supported | Untestable |
x.splitText(5) Split the text node x at the 6th character. x now contains the first part (char. 0-5), while a new node is created (and becomes x.nextSibling) which contains the second part (char. 6-end) of the orginial text. |
||||
substringData() | Supported only on Mac IE6! | Supported | Supported | Untestable |
x.substringData(4,3) Takes a substring of x, which must be a text node, starting at the fifth character and with a length of three characters. Thus it's the same as the old substr() method of strings. Since substringData() isn't supported by Explorer 5 on Windows, I advise you to use y.data.substr(4,3) instead. |
||||
swapNode() | Supported only on Windows | Not supported | Not supported | Not supported |
x.swapNode(y) Put node x in node y's place and vice versa. |
||||
tagName | Supported | Supported | Supported | Supported |
x.tagName The name of node x, which must be an element (HTML tag). |
||||
title | Supported | Supported | Supported | Supported |
x.title The title attribute of node x. Can also be set: x.title = 'Other title!' |
||||
value | Supported only on Mac IE6! | Supported | Supported | Untestable |
x.value The value of x, which must be an attribute. |
If you know of a method or property not listed here, please mail me.
I noticed that as soon as you start to generate JavaScript events, like adding a mouseover to a link, you are in deep trouble. Below are some example scripts and their results.
The goal of each of the scripts below is to get this HTML tag
<A HREF="#" onMouseOver="alert('Correct!')>Link!</A>
and to see if the mouseover gives an alert.
Script | Explorer 5.0 and 6.0 | Netscape 6.01 | Konqueror 2.1 | |
---|---|---|---|---|
Generate tag |
var a = document.createElement('A'); a.setAttribute('onmouseover','alert("Correct!")'); a.setAttribute("href","#"); b = document.createTextNode('Link!'); a.appendChild(b); document.getElementsByTagName('BODY')[0].appendChild(a); |
No alert box | Alert box | Alert box |
Add attributes to existing tag | Existing tag:
<A></A>Add attributes and text. var a = document.getElementsByTagName('a')[0]; a.setAttribute('onmouseover','alert("Correct!")'); a.setAttribute("href","#"); b = document.createTextNode('Link!'); a.appendChild(b); |
No alert box | Alert box | Alert box |
Change attributes of existing tag | Existing tag:<A HREF="#" onMouseOver="alert('Incorrect')">Link!</A>Change mouseover attribute. var a = document.getElementsByTagName('a')[0]; a.setAttribute('onmouseover','alert("Correct!")'); |
No alert box | Alert box | Alert box |
Clone existing tag | Existing tag:<A HREF="#" onMouseOver="alert('Correct')">Link!</A>Clone tag and see what happens to the copy. var a = document.getElementsByTagName('a')[0].cloneNode(true); document.getElementsByTagName('BODY')[0].appendChild(a); |
Alert box only on Windows | No alert box | Alert box |