Ajax JavaScript CSS HTML 5 Gecko XUL Forum

DOM Tutorial

The Document Object Model allows interfacing programming languages with XML documents
by associating them with an object oriented model.
It describes the structure of an XML or HTML document, a web page and allows access to each individual element. We will see in this tutorial that you can modify each element of a page, or create a page or a part through DOM's methods.

This tutorial is based on the DOM 2 Core specification implemented by all browsers

document object model

Throught DOM you can access the document

How to use DOM?

DOM is an API, a set of objects with their attributes and methods, an interface for JavaScript.

The document object

This is a DOM object corresponding to the current page. However some tags as <iframe> <browser> and <tabbrowser> can introduce other documents into the same window.

Essential methods and attributes of document

Essential dynamic methods of document

Example of use with JavaScript

var anchorList = document.getElementsByTagName("a") ; 
for(var i = 0; i < anchorList.length; i++)
{ 
 alert("href: " + anchorList[i].href + "\n"); 
} 

This example parses a web page to find links and display them.
- document is an object defined in core DOM to represent the document.
- getElementsByTagName is a method of the object wich builds an array from each tag matching the parameter, "a" for instance.
- href is a property of HTML DOM.
- anchorList is a declared variable.

DOM interface

We will see the details of interfaces and how to use them in examples: list of attributes, list of methods, access to the content, add elements (example: Include another page), modify the contents, remove items.

Case studies

DOM's objects and properties are sufficient to change dynamically graphical element in a webpage.

See also

References

© 2009-2011 Xul.fr