The Document Object Model (DOM) allows JavaScript code to access and manipulate HTML documents through DOM nodes. DOM nodes can be of various types, such as element nodes and text nodes, and each node has a nodeType property giving its type. The DOM specification defines twelve constants corresponding to these types, which can be accessed through the Node object in compatible browsers.
Unfortunately Internet Explorer still does not provide this object, more than a decade after the DOM specification was released and despite two Microsoft employees being editors of the specification. The page provides a small piece of JavaScript that can be copied into your code to ensure these constants are available in all browsers.
The code
Copy the following code to your JavaScript, adding it before your first use of the constants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
An example of using nodeType constants
The following code outputs any comments contained within the HTML source code of a page. It uses the nodeType constants to identify element nodes (in which it recursively searches) and comment nodes (whose content it outputs).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|