Netscape Navigator JavaScript comes with a number of built-in objects. Although
you cannot create new objects or object classes, as you can in languages such
as Java, it is useful to understand the object hierarchy of the built-in
objects. In the strict object-oriented sense, this type of hierarchy is known
as an instance hierarchy, since it concerns specific instances of
objects rather than object classes.
The hierarchy is illustrated below:
window
|
+--parent, frames, self, top
|
+--location
|
+--history
|
+--document
|
+--forms
| |
| form elements (text fields, textareas, checkboxes,
| radio buttons, selections, buttons)
+--links
|
+--anchors
|
+--plug-ins (NYI)
|
+--applets (NYI)
|
+--elements (NYI)
The document object and all objects above it in the hierarchy are
always present. Everything below the document object, such as
forms, form elements, and anchors, are created based on
the contents of the HTML page. For example, a form object is
created for each form in the document.
NOTE: Currently (in beta2), you must refer to forms as an array: The
first form is document.forms[0], the second is document.forms[1],
and so on. Ultimately, you will be able to name forms and refer to them by
their names.
To refer to specific properties of these objects, you must specify the object
name and all its ancestors. For example, suppose you have a page with one
form, and the form contains this text field:
Events are actions that occur, usually as a result of some user
action. For example, a button click is an event, as is giving focus to a form
element. Event handlers are scripts that are executed
automatically when an event occurs. Event handlers are written in HTML as
attributes of form elements to which you assign JavaScript code to execute when
the event occurs.
For example, suppose you have created a JavaScript function called
compute(). You can cause the script to execute when the user clicks on a
button by assigning its onClick attribute to the function call, as follows:
Notice the use of this.form to refer to the current form. The keyword
this refers to the current object; in the above example, it would be
the button object. The construct this.form then refers to the form
containing the button. In the above example, the onClick event handler is a
call to the compute() function, with this.form, the current form,
as the parameter to the function. Instead of calling a function, you can put
any JavaScript statements to be executed inside the quotes.
Events apply to form elements as follows:
Focus, Blur, Change events: text fields, textareas, and selections.
A method is a function associated with an object. You execute
a method as follows:
object.method()
The following predefined methods emulate events; they are applicable
to form elements if the event they emulate is applicable. See "JavaScript
Event Handlers," above.
The history contains information on the URLs that the client
has visited. This information is stored in a history list, and is
accessible through the Go menu in the Navigator.
Properties
back
URL of previous history entry.
forward
URL of next history entry.
current
URL of current page.
length
Length of the history list.
Methods
go(delta)
The argument delta is an integer. If delta is
greater
than zero, then it loads the URL that is that number of entries forward
in the history list; otherwise, it loads the URL that is that number of
entries backward in the history list.
go("string")
Go to the newest history entry whose title or URL contains string as
a substring; substring matching is case-insensitive.
toString()
Return a string consisting of an HTML table with links to all entries in
the history list.
The document object contains information on the current
document.
Properties
title
Current document title (if undefined, "Untitled").
location
The full URL of the document.
lastmodified
A string containing the last-modified date.
loadedDate
NYI.
referer
NYI.
bgColor
RGB value of background color, expressed as a hexidecimal triplet.
fgColor
RGB value of foreground (text) color, expressed as a hexidecimal triplet.
linkColor
RGB value for color of hyperlinks, expressed as a hexidecimal triplet.
vlinkColor
RGB value for color of visited links, expressed as a hexidecimal triplet.
alinkColor
RGB value for color of activated links (after mouse-button down, but
before mouse-button up), expressed as a hexidecimal triplet.
forms[index]
Array of form objects, in source order. There will be a form object
for each form in the document.
forms.length
The number of form objects in this document.
links[index]
Array objects corresponding to all HREF links in source order.
links.length
The number of link objects (HREF links) in this document.
anchors[index]
Array of objects corresponding to named anchors (
tags) in source order.
anchors.length
The number of anchor objects (named anchors) in this document.
Methods
write()
Write HTML to the current window, in sequence with the HTML containing
this SCRIPT. SCRIPTs have no effect on paragraphs or other structures in
which they may occur.
writeln()
The same as write(), but adds a carriage return. Note that this
only
affects preformatted text (inside a
Each form in a document corresponds to a distinct object.
These objects are maintained in an array called forms, so the first form
in a document is
document.forms[0], the second is document.forms[1], and so on.
You then
refer to the form elements in each form by that name. For example, you would
refer to
a text field named
quantity in the second form as:
document.forms[1].quantity
You would refer to the value property of this form element object as:
document.forms[1].quantity.value
Properties
name
String value of NAME attribute. NYI.
method
Value of METHOD attribute, "get" or "post" (0 or 1).
action
String value of ACTION attribute.
target
Window targeted for form response after the form has been submitted.
Event Handlers
onSubmit()
Method, JavaScript code to run when the form is submitted.
Radio buttons all have the same NAME attribute, so the
JavaScript reflection
of the button-group is a single object that has numbered properties starting
from zero, one for each button.
For example, the following defines a radio button group to choose
among three catalogs, and a text field that defaults to what was chosen
via the radio buttons but that allows the user to type a nonstandard
catalog name as well. The JavaScript automatically sets the catalog name
input field based on the radio buttons.
Submit buttons, that cause the form to be submitted
Reset buttons, that reset all elements in the form to their defaults
Custom buttons, to perform any other function
For all of these buttons, the VALUE attribute specifies the text to display on
the face
of the button.
A submit button is defined as in the following example:
Clicking on this button will submit the form to the program specified by the
ACTION attribute of
the form. This action will always load a new page into the client; it may be
the same as the
current page, if the action so specifies.
A reset button is defined as in this example:
A custom button is defined as in this example:
A custom button does not neccessarily load a new page into the client; it merely executes the script specified by the onClick attribute. In this example, myfunction() is a JavaScript function.
All links in a document are maintained in the links array; the
first link
is link[0], the second is link[1], and so on.
Each link object is a location object.