In the last chapter, we saw how to write DHTML for earlier browsers, such as IE 4 and Netscape 4. We also saw how different the code for the browsers had to be used to achieve the same results, making the web developer's life a difficult one. In this chapter we'll be concentrating on writing code that works with IE 5.5 and later and Netscape 6 and later. However we'll be sticking with web standards set by the W3C and we'll see this makes life a lot easier. In general, we can write code that will work with both IE 5.5/6 and Netscape 6/7 without having to make big changes.
The DOM should not be confused with the Browser Object Model (BOM) that we introduced in Chapter 5 and used extensively in the last chapter. We'll look at the differences between the two in detail shortly. For now though, think of the BOM as a browser-dependent representation of every feature of the browser, from the browser buttons, URL address line, title bar, to the browser window controls, as well as parts of the web page too. The DOM, however, deals only with the contents of the browser window or web page, in other words, the HTML document. It makes the document available in such a way that any browser can use exactly the same code to access and manipulate the content of the document. To summarize, the BOM gives access to the browser and some of the document, whereas the DOM gives access to all of the document, but only the document.
The DOM does this by representing the contents of the page as a generic tree structure. Whereas in the BOM, we might expect to access something by looking up a property relevant to that part of the browser and adjusting this, the DOM requires us to navigate through its representation of the page via nodes and properties nonspecific to the browser. We'll explore this structure a little later.
However, to use the DOM standard, ultimately we require browsers that completely implement the standard, something that no browser does 100% efficiently. Unfortunately, IE 5.5 didn't support many aspects of the standard. IE 6 has improved on that situation but still falls short of full implementation. Even NN 6 and 7, which at least aim to support the standard, still fall short in some ways. So while cross-browser coding is much simpler for NN 4 and IE 4, it is still not as simple as it could be.
This makes the DOM sound like an impossible ideal, yet the DOM doesn't exist purely as a standard. Features of the DOM standard have been implemented in browsers as far back as NN 2. However, in NN versions 2, 3, and 4, many HTML page elements and their properties were not scriptable at all, while, as we have seen, IE 4 made nearly all page elements and their properties scriptable. Unfortunately, the way in which NN 4 provides scripting access to some elements is often incompatible with the way in which IE 4 makes those elements available. Thus a Document Object Model standard was developed. The latest versions of IE and NN browsers support many features outlined in the standard.
The HTML, ECMAScript, XML, and XHTML Web Standards
The DOM standards
The DOM tree structure
Writing Cross-Browser DHTML
Remember that the examples within this chapter are targeted only at the DOM and therefore will only be supported by IE 5.5 and later, and NN 6 and 7.