PPK on JavaScript: The DOM - Part 2/Page 4 | 2 | WebReference

PPK on JavaScript: The DOM - Part 2/Page 4 | 2

[previous] [next]

PPK on JavaScript: The DOM - Part 3

J: Forms and the Level 0 DOM

Until now, we've discussed the W3C Level 1 DOM. All browsers, however, also support the Level 0 DOM, which is part of the de facto Netscape 3 standard. This DOM is more limited than the W3C Level 1 DOM, and it has been almost completely superseded by the newer one, which is far more versatile and generalized. There's one exception: the Level 0 DOM remains better equipped for working with forms and form fields than the W3C DOM.

Level 0 DOM nodeLists The Level 0 DOM also works with nodeLists, but they differ from Level 1 DOM nodeLists in four important respects:
  • Level 0 DOM nodeLists are available only for a limited number of elements, most importantly form fields, images, and links.
  • Level 0 DOM nodeLists allow you to search for an element by name or ID, in addition to the index numbers we already discussed in 8I.
  • One Level 0 DOM nodeList, options[], allows you to create and remove option elements. It is effectively a read/write array.
  • The Level 0 DOM nodeList elements[] has no counterpart in the Level 1 DOM.

Take this HTML snippet:

The Level 1 DOM allows us to access these images by document.getElements ByTagName('img'). The Level 0 DOM allows the same through the document.images[] nodeList. document.images[0] is the same as document.getElementsByTagName('img')[0].

In addition, the Level 0 DOM allows document.images['firstIMG'] and document.images['secondIMG']. It searches for the image with the given name or ID. That can occasionally be quite useful, especially if you have to find a specific image and are not sure of the exact document structure (because other scripts might have changed it, for instance). Just as Level 1 DOM nodeLists do, document.images[] reflects all changes in the document, so it presents the same dangers.

document.images[] is nothing more than a convenient alternative to document.getElementsByTagName('img'). The other common Level 0 DOM nodeLists are somewhat more than that.

document.firstIMG: The Level 0 DOM even allows you to say document.firstIMG, i.e., to use the name of the image (or form field or whatever) as a property of document.

Even back in the Netscape 3 days I didn't like this syntax, because it obscures what you're doing. Take this: document.income. Is this an image, a form, a link, or what? Other JavaScript programmers who have to work with your scripts won't know right away, whereas document.forms['income'] is totally clear to everyone.

Therefore, I advise you not to use this syntax.

First of all, there are document.links[] and document.anchors[]. These contain all <a href> and <a name> tags in your pages, respectively. In contrast, document.getElementsByTagName('a') contains both kinds of tags. Although I've never needed them, it's still remotely possible that these old nodeLists may come in useful in a few situations.

[previous] [next]