Ajax JavaScript CSS HTML 5 Gecko XUL Forum

HTML Document - Accessing content of a webpage

The Document object defined in the DOM specification provides access to the contents of an HTML page or an XML file. The most commonly used methods of the object are getElementByTagName and getElementById while write comes from the HTMLDocument interface, which will be used instead for webpages.

Purpose of the object

The document represents the whole page, its attributes indicate how it is defined and its methods allow access to its content.
It is used with the keyword document:

document.write("démo")

Attributes of Document

These are read-only attributes defined when creating the page.

Attributes of HTMLDocument

The HTMLDocument interface is derived from Document. You can access HTMLDocument by the name document (and therefore also to Document that is the superclass of HTMLDocument).
You can access the attributes and collections by the following names:

The collections are accessed by an index as an array for example: form [x].

Methods of Document

The DOM methods can take for parameter a DOMString object, which in practice is a simple string in JavaScript.

Methods of HTMLDocument

In addition to the methods of Document, HTMLDocument offers additional functions to directly alter the content of the page.

Note that we place text in HTML tags by assigning the innerHTML attribute. The write method is used to create a new document in a dynamic way.

NodeList

This interface has one attribute and a single method.

Node

A node is any element of the tree of the document, possibly contained in another element, and which may contain other elements (except for Text which can not have children).

We do not describe here this interface that requires an entire chapter. Some attributes and methods are necessary to use a static document. They are all read-only.

It is possible to assign a Node to an Element and vice-versa, that allows to use the methods of one or the other depending on need. To access the attributes (for example href in the case of a tag), we will use the element.

HTMLCollection

A collection represents a list of HTML tags in a page, such as images, links, etc..

The methods return null in case of failure.

Demonstration

Demonstration of methods of the DOM object Document and the HTMLDocument interface (which inherits) showing how to access data in the document.

Click a button to see data that can be obtained with these interfaces. The source code associated to the button is displayed on the right.

onclick="alert(document.title)"

onclick="alert(document.URL)"

onclick="alert(document.domain)"

onclick="alert(document.referrer)"

onclick="alert(document.anchors.item(0))"
Note: without a name attribute, anchors are not included.

onclick="alert(document.links.item(0))"
All links are included.

onclick="alert(document.doctype.name)"

Specifications from the W3C

© 2008-2011 Xul.fr