HTML and CSS Reference
In-Depth Information
chapter
15
The W3C DOM
and JavaScript
15.1 The W3C DOM
The W3C DOM provides JavaScript applications a standard way to access all the ele-
ments of the document. Although the DOM was designed to help developers navigate
an XML document, HTML documents can be navigated in the same way. For JavaScript
to change the style of a document dynamically, the HTML elements must be represented
as objects. We have already discussed the way JavaScript views the hierarchical tree-like
structure of the document object model (DOM Level 0) in Chapter 11, “Working with
Forms and Input Devices.” The W3C DOM, starting with DOM 1, expanded the DOM
to include every XML/HTML element of the page. With this DOM (see Figure 15.1), all
HTML elements, attributes, and text become objects and the DOM provides methods
and properties allowing not only access to all these objects, but the ability to use CSS to
style them on the fly. With the W3C DOM, a Web page can be restructured by creating, add-
ing, modifying, and deleting any item on the page. An excellent source describing the DOM
objects and methods is http://www.howtocreate.co.uk/tutorials/JavaScript/domstructure.
Today the term unobtrusive JavaScript shows up in tutorials, blogs, books, and dis-
cussion groups when discussing Web development techniques. The goal is to com-
pletely, if possible, separate JavaScript code from the other two layers of a Web page
by putting all the JavaScript functionality in separate .js files independent of the
markup and CSS. This allows the programmer to make updates in one place, and to
avoid interfering with those using unsupported browsers or those who have disabled
JavaScript. The DOM Level 1 provided a way to do this by turning every element of a
Web page into an object.
611
 
 
 
Search WWH ::




Custom Search