The hierarchy of JavaScript Objects
|
OBJECT | DESCRIPTION |
WINDOW | the top-level object; contains properties that apply to the entire window. There is also a window object for each for "child window" in a frames document. |
demonstrates methods: alert(), confirm() and prompt() | |
demonstrates opener property | |
opening and closing new windows | |
bringing new window to the front and back. | |
DOCUMENT | contains properties for content in the current document, such as title, background color, and forms |
changes properties of foreground and background color based on user input | |
HISTORY | contains properties representing URLs the user has previously visited |
![]() |
|
LOCATION | contains properties on the current URL |
![]() |
|
![]() |
The properties of the document Object are largely content-dependent. That is, they are created based on the content that you put in the document. For example, the document Object has a Property for each Form and each Anchor in the Document.
For example, suppose you create a page named simple.htm that contains the following HTML:
<HTML>
<HEAD><TITLE>A
Simple Document</TITLE></HEAD>
<BODY bgColor="White">
<FORM NAME="myform" METHOD="POST" ACTION="/cgi-bin/mail.cgi"></BODY>
</HTML>
As always, there would be window, location, history, and document objects. These would have properties such as:
These are just some example values. In practice, these values would be based on the document's actual location, its title, foreground and background colors, and so on.
Notice that the value of document.title reflects the value specified in the <TITLE> tag. The value for document.bgColor (the background color) was set in the HTML while the document.fgColor (the color of text) was not set in the HTML, so this value is based on the default values specified in the Preferences dialog box (when the user chooses General Preferences from the Options menu).
Because there is a Form in the document, there is also a form Object called myform (based on the Form's NAME attribute) that has 3 child objects: 1) text input field, 2) checkbox, and 2) button. Each of these objects has a name based on the NAME attribute of the HTML tag that defines it, as follows:
These are the full names of the objects, based on the DOM Hierarchy.
NOTE: JavaScript is case sensitive document.myform.Check1 is not the same "Object" as document.myform.check1. In the example above document.bgColor is the correct way to deal with the document's background color but document.bgcolor is not!
Thus, the form Object myform has properties based on the attributes of the <FORM> tag, for example,
The Form Object has child objects named Button1 and text1, corresponding to the button and text field in the Form. These objects have their own properties based on their HTML attribute values, for example,
Three different ways to specify an Object's value:
In practice, you refer to these properties using their full names, for example, document.myform.button1.value. This full name is based on the Navigator Object Hierarchy, starting with document, followed by the name of the form, myform, then the element name, button1, and, finally, the property name.
To finish off here are some additional information we can get from the document and Form.
This simple HTML document creates an anchor, a small Form, and a link to that anchor. This is not intended to be the HTML for a meaningful Web page, but it will nevertheless illustrate the correspondence between HTML elements and JavaScript HTML objects.
<HTML>
<HEAD><TITLE>A
very simple HTML page</TITLE></HEAD>
<BODY>
<A NAME="top">This
is the top of the page</A>
<HR>
<FORM
METHOD="POST"
ACTION="mailto:john@tropicalpenguin.com"><P>
Enter your name: <INPUT
TYPE="text"
NAME="me"
SIZE="70"></P>
<INPUT
TYPE="Submit"
VALUE="OK">
<INPUT TYPE="Reset"
VALUE="Oops">
</FORM>
<HR>
Click here to go to the <A
HREF="#top">top</A>
of the page
</BODY>
</HTML>
This code creates an HTML page with an anchor at the top of the page and a link to that anchor at the bottom. In between is a simple form that allows the user to enter his name. There is a submit button if he gets it right, and a reset button if he doesn't. If the user is successful the form's contents are submitted via a post action to the fictitious e-mail address john@tropicalpenguin.com.
The important aspect of this example is not its primitive HTML, but the fact that the HTML elements in it are reflected in the JavaScript Document Object Model (DOM) hierarchy. We have already seen that we can access the title of this document through the title property of the Document Object. We can also access the other HTML elements of this document using the following properties:
These properties of the Document Object are Arrays representing every HTML element that is an Anchor, Form, or Link on the page. In our particular example there is only one of each, so we would refer to the Anchor at the top of the page as document.anchors[0], the link at the bottom of the page as document.links[0], and the form in the middle of the page as document.forms[0]. These are the top-level HTML objects represented by this document. Each of these elements, in turn, has Properties and Methods that can be used to describe and manipulate it.
In particular, the Form Object corresponding to forms[0] has sub-objects for each of the 3 Form Elements (the reset button, the submit button, and the text input field), as well as properties for the submit method and the submit target. forms[0].elements[0] corresponds to the text input field. forms[0].elements[0].name is the name of that field, as specified by the NAME field, which is "me" in this case. The figure below recapitulates this HTML code and shows how each element in the page is associated with an HTML object.
document.title
|
<TITLE>A very simple HTML page</TITLE>
document.anchors[0]
|
<A NAME="top">This
is the top of the page</A>
|
document.anchors[0].name
document.forms[0]
document.forms[0].action
|
|
<FORM
METHOD="POST"
ACTION="mailto:john@tropicalpenguin.com">
|
document.forms[0].method
document.forms[0].elements[0] _____ document.forms[0].elements[0].name
|
|
<INPUT TYPE="text"
NAME="me"
SIZE="70">
|
document.forms[0].elements[0].type
document.forms[0].elements[1]
___ document.forms[0].elements[1].value
|
|
<INPUT
TYPE="Submit"
VALUE="OK">
|
document.forms[0].elements[1].type
document.forms[0].elements[2]
___ document.forms[0].elements[2].value
|
|
<INPUT
TYPE="Reset"
VALUE="Oops">
|
document.forms[0].elements[2].value
document.links[0]
|
<A HREF="#top">top</A>
of the page
|
document.links[0].href