DOM - Part one
What's 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.The Level 1 DOM Recommendation has been developed by the W3C to provide any programming language with access to each part of an XML document. As long as you use the methods and properties that are part of the recommendation, it doesn't matter if you parse an XML document with VBScript, Perl or JavaScript. In each language you can read out whatever you like and make changes to the XML document itself.
As some of you might have guessed, this paragraph describes an ideal situation and differences (between browsers, for instance) do exist. Generally, however, they're far smaller than usual so that learning to use the W3C DOM in JavaScript will help you to learn using it in another programming language.
In a way HTML pages can be considered as XML documents. Therefore the Level 1 DOM will work fine on an HTML document, as long as the browser can handle the necessary scripts.
You can read out the text and attributes of every HTML tag in your document, you can delete tags and their content, you can even create new tags and insert them into the document so that you can really rewrite your pages on the fly, without a trip back to the server.
Because it is developed to offer access to and change every aspect of XML documents, the DOM has many possibilities that the average web developer will never need. For instance, you can use it to edit the comments in your HTML document, but I don't see any reason why you would want to do so. Similarly, there are sections of the DOM that deal with the DTD/Doctype, with DocumentFragments (tiny bits of a document) or the enigmatic CDATA. You won't need these parts of the DOM in your HTML pages, so I ignore them and concentrate instead on the things that you'll need in your daily work.
How DOM works?
In DOM, every element and its content was seen as nodes in a "document tree". Say we have codes like this:
<p>This is a paragraph<p>You'll have a tree as below:
<P> <-- element node
|
|
This is a paragraph >-- text node
And if we have:
<p>This is a <b>paragraph</b></p>
We'll have a tree like this:
<P>
|
--------------
| |
This is a <B>
|
|
paragraph
We can see that if a tag is nested inside another, it will become a child node of its outer tag. So a DOM tree is formed by creating children nodes below parents as they are nested inside other nodes. By doing this we can guarantee that a well-formed web page will be mapped to definite, well-form DOM tree.
How to walk through a DOM tree?
Knowing the exact structure of the DOM tree, you can walk through it in search of the element you want to influence. For instance, assume the element node P has been stored in the variable
x (later on I'll explain how you do this). Then if we want to access the BODY we dox.parentNodeWe take the parent node of x and do something with it. To reach the B node:
x.childNodes[1]
childNodes is an array that contains all children of the node x. Of course numbering starts at zero, so childNodes[0] is the text node 'This is a' and childNodes[1] is the element node B.
Two special cases: x.firstChild accesses the first child of x (the text node), while x.lastChild accesses the last child of x (the element node B).
So supposing the P is the first child of the body, which in turn is the first child of the document, you can reach the element node B by either of these commands:
document.firstChild.firstChild.lastChild;
document.firstChild.childNodes[0].lastChild;
document.firstChild.childNodes[0].childNodes[1];
etc.
or even (though it's a bit silly)
document.firstChild.childNodes[0].parentNode.firstChild.childNodes[1];


0 Comments:
Post a Comment
<< Home