Level 0 DOM

 

The Document Object Model (DOM) is the model that describes how all elements in an HTML page, like input fields, images, paragraphs etc., are related to the topmost structure: the document itself. By calling the element by its proper DOM name, we can influence it.
This page gives some general info about the various DOMs and then describes the Level 0 DOM.

First of all a little introduction to the Document Object Model, followed by a bit of history. Then we'll take a look at accessing elements through the Level 0 DOM and how to use the Level 0 DOM.

Document Object Model

The Document Object Model has been around since browsers support JavaScript. From Netscape 2 onwards, web programmers wanted to access bits of HTML and change its properties. For instance, when you write a mouseover script you want to go to a certain image on the page and change its src property. When you do this, the browser reacts by changing the image on the screen.

The function of the Document Object Model is to provide this kind of access to HTML elements on your page. Exactly what elements you can access in which way and exactly what you can change depends on the browser. Each higher browser version gives you more freedom to reach any element you like and change anything you like.

DOM history

There are three DOM levels:

  1. The Level 0 DOM, supported from Netscape 2 onwards by all browsers.
    This DOM is treated below on this page.
  2. The two Intermediate DOMs, supported by Netscape 4 and Explorer 4 and 5.
    These DOMs are treated on the Intermediate DOMs page.
  3. The Level 1 DOM, supported by Netscape 6 and Explorer 5.
    This DOM is treated on the Level 1 DOM page.

Now let's take a look at the origins and development of the Document Object Model.

Level 0 DOM

The Level 0 DOM was invented by Netscape at the same time JavaScript was invented and was first implemented in Netscape 2. It offers access to a few HTML elements, most importantly forms and (later) images.

For reasons of backward compatibility the more advanced browsers, even those who support the Level 1 DOM, still also support the old, faithful Level 0 DOM. Not supporting it would mean that the most common scripts suddenly wouldn't work any more. So even though the Level 0 DOM doesn't entirely fit into the new DOM concepts, browsers will continue to support it.

For the same reason Microsoft was at first forced to copy the Netscape DOM for Explorer 3. They wanted a real competitor for Netscape and having it produce lots of error messages on every page that contained JavaScript would have been strategically unsound.

Therefore the Level 0 DOM is really unified: all browsers that support parts of it support these parts in the same way. With the later DOMs this situation changed.

Intermediate DOMs

When the Version 4 browsers were released, the hype of the day was DHTML so both browsers had to support it. DHTML needs access to layers, separate parts of a page that can be moved across the page. Not surprisingly in view of their increasing competition, Netscape and Microsoft chose to create their own, proprietary DOMs to provide access to layers and to change their properties (their position on the page, for instance). Netscape created the layer model and the DOM document.layers, while Microsoft used document.all. Thus a proper cross-browser DHTML script needs both intermediate DOMs.

Much has been argued about this split between the browsers. When all's said and done, the Netscape DOM is the most complicated one, hard to use and buggy, while the Explorer DOM is more targeted towards web developers, relatively easy to use but crippled by lack of documentation. Personally I like neither of them and I'm glad they're on the way out.

Level 1 DOM

Meanwhile W3C had developed the Level 1 DOM specification. The Document Object Model W3C proposed was at first written for XML documents, but since HTML is after all a sort of XML (though purists will shudder at this thought), it could serve for browsers too.

Besides, the Level 1 DOM is a real advance. For the first time, a DOM was not only supposed to give an exact model for the entire HTML (or XML) document, it is also possible to change the document on the fly, take out a paragraph and change the layout of a table, for instance.

Since both Netscape and Microsoft had participated in the specification of the new DOM, since both browser vendors wanted to support XML in their version 5 browser and since public pressure groups like the Web Standards Project exhorted them to behave sensibly just this once, both decided to implement the Level 1 DOM.

Of course, this doesn't mean that Netscape 6 and Explorer 5 are the same. Again for reasons of backward compatibility Microsoft decided to continue support of document.all so that Explorer 5 now supports two DOMs (three if you count the Level 0 DOM).
On the other hand, the core of Netscape 6 is being built by the open source Mozilla Project and the leaders of this project have decided to completely ditch the old document.layers DOM of Netscape 4 and have Netscape 6 support only the Level 1 DOM.

See the Level 1 DOM page for more information.

Accessing elements

Each DOM gives access to HTML elements in the document. It requires you, the web programmer, to invoke each HTML element by its correct name. If you have done so, you can influence the element (read out bits of information or change the content or layout of the HTML element). For instance, if you want to influence the image with NAME="first" you have to invoke it by its proper name

document.images['first']

and you are granted access.

Each element name begins with document.. All HTML elements are inside the document in some way or another. In the higher level DOMs the relation of the element to the document can become quite complex. For instance, to access an image 'first' in a layer 'second' in Netscape 4, do

document.layers['second'].document.images['first']

How to use the Level 0 DOM

The structure of the Level 0 DOM is simple, though. At the top is the document and within the document it gives access to images, forms, links and anchors (Links are <A HREF>s, anchors are <A NAME>s). In practice, images and forms are often used, links and anchors are hardly used.

When the browser concludes that an HTML document has been completely loaded, it starts making arrays for you. It creates the array document.images[] and puts all images on the page in it, it creates the array document.forms[] and puts all forms on the page in it etc.

This means that you now have access to all forms and images, you just have to go through the array in search of the exact image or form that you want to influence. This can be done in two ways: by name or by number.

Suppose you have this HTML document:

-------------------------------------------
|                document                 |
| --------         -------------------    |
| |img   |         |   second image  |    |
| --------         |                 |    |
|                  -------------------    |
|  -------------------------------------  |
|  |             form                  |  |
|  | ---------------------             |  |
|  | |      address      |             |  |
|  | ---------------------             |  |
|  -------------------------------------  |
-------------------------------------------
document.images

The first image has NAME="thefirst", the second has NAME="thesecond". Then the first image can be accessed by either of these two calls:

document.images['thefirst']
document.images[0]

The second one can be accessed by either of these calls:

document.images['thesecond']
document.images[1]

The first call is by name, simply fill in the name (between quotes, it's a string!) within the [] and you're ready.

The second call is by number. Each image gets a number in the document.images array, in order of appearance in the source code. So the first image on a page is document.images[0], the second one is document.images[1] etc.

document.forms

The same goes for forms. Suppose the form on the page has NAME="contactform", then you can reach it by these two calls:

document.forms['contactform']
document.forms[0]

But in the case of forms, usually you don't want to access just the form, but a specific form field. No problem, for each form the browser automatically creates the array document.forms[].elements[] that contains all elements in the form.

The form above holds as first element an <INPUT NAME="address">. You can access it by these four calls:

document.forms['contactform'].elements['address']
document.forms['contactform'].elements[0]
document.forms[0].elements['address']
document.forms[0].elements[0]

These four calls are completely interchangeable, it's allowed to first use one, then another. It depends on your script exactly which method of access you use.

Doing what you need to do

Once you have correctly accessed a form field or an image through the Level 0 DOM, you'll have to do what you want to do. Images are usually accessed to create a mouseover effect that changes the property src of an image:

document.images['thefirst'].src = 'another_image.gif';

Usually you want to access forms to check what a user has filled in. For instance to read out what the user has filled in check the property value:

x = document.forms[0].elements[0].value;

and then check x for whatever is necessary. See the introduction to forms for details on how to access specific form fields (checkboxes, radio buttons etc.).

 

This is how the Level 0 DOM works. If you want to continue the DOM trilogy, you should now read the Intermediate DOMs page.

Home