Undertanding The Methods And Properties Of The JavaScript Document Object (Joe Thomas)
May 31, 2010
In JavaScript, a new document object is generated each time a page is loaded into a browser. Like all objects, the document object exposes several methods and properties which can be accessed programmatically. One common document method is the write() method which is used to output a text string to an HTML document. The document object forms part of the Document Object Model (DOM), a logical hierarchy representing an HTML page and the elements it displays which makes it possible for scripting languages like JavaScript to access and manipulate elements.
The DOM views the HTML document as being composed of nodes, each of which can be accessed in a number of ways. For example, a node can be viewed as child or parent of another node or it could be referred to by its ID or its name. Since the document represents the entire HTML page, many of its properties are actually collections of other objects. For example, the anchors property is an array of all the named anchor objects on a page.
The document object has a cookie property which is used to set and read cookies, small files which are stored on the user's computer (with their permission) and which can store information such as the user's preferences. When writing information, the cookie property is set to a string representing the information; for example document.coookie = "username=mikki".
There is also a domain property which automatically holds the domain name which contains the page currently being displayed in the browser. The value of document.domain can be read at any time to obtain this information.
The forms and images properties of the document object return arrays containing all the forms and images on a page. To count the number of forms or images on a page, you can use the length property of these arrays: document.forms.length or document.images.length. A length of zero would indicate that no forms/images are present on the page.
In a similar way, the document object has a links property which is an array containing all the hyperlinks on a page. Hyperlinks may be placed on text, images or imagemap hotspots. As with other arrays, you can use the length property to count the number of hyperlinks on a page.
Let us finish by mentioning the referrer property. This useful document object property holds the value of the URL which the user was viewing prior to accessing the current page. This is only useful when the user has clicked a link to get to the page. If the user entered the URL directly into the browser address bar or used on of his or her bookmarks, no value would be returned.
About the AuthorIf you want more information InDesign training courses, visit Macresource Computer Training, a UK IT training company offering InDesign Classes in London and throughout the UK..