The following objects are available in JavaScript:
|
|
|
|
NOTE: Each object topic indicates whether the object is part of the client (in Navigator), server (in LiveWire), or is common (built-in to JavaScript). Server objects are not included in this version of the documentation.
A piece of text identified as the target of a hypertext link.
To define an anchor, use standard HTML syntax:
<A NAME="anchorName" [HREF=locationOrURL]> anchorText </A>NAME="anchorName" specifies a tag that becomes an available hypertext target within the current document.
You can reference the anchor objects in your code by using the anchors property of the document object. The anchors property is an array that contains an entry for each anchor in a document. See the anchors property.
None.
See also the example for the anchors property.
A pushbutton on an HTML form.
To define a button:
<INPUT TYPE="button" NAME="buttonName" VALUE="buttonText" [onClick="handlerText"]>NAME="buttonName" specifies the name of the button object. You can access this value using the name property.
To use a button object's properties and methods:
1. buttonName.propertyName 2. buttonName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)buttonName is the value of the NAME attribute of a button object.
A button object is a form element and must be defined within a <FORM>...</FORM> tag.
The button object is a custom button that you can use to perform an action you define. The button executes the script specified by its onClick event handler.
In the following example, myfunction() is a JavaScript function.
A checkbox on an HTML form. A checkbox is a toggle switch that lets the user set a value on or off.
To define a checkbox, use standard HTML syntax with the addition of the onClick event handler:
<INPUT TYPE="checkbox" NAME="checkboxName" VALUE="checkboxValue" [CHECKED] [onClick="handlerText"]> textToDisplayNAME="checkboxName" specifies the name of the checkbox object. You can access this value using the name property.
To use a checkbox object's properties and methods:
1. checkboxName.propertyName 2. checkboxName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)checkboxName is the value of the NAME attribute of a checkbox object.
A checkbox object is a form element and must be defined within a <FORM>...</FORM> tag.
Use the checked property to specify whether the checkbox is currently checked. Use the defaultChecked property to specify whether the checkbox is checked when the form is loaded.
The following example displays a group of four checkboxes that all appear checked by default.
Lets you work with dates and times.
To create a Date object:
1. dateObjectName = new Date()
2. dateObjectName = new Date("month day, year hours:minutes:seconds")
3. dateObjectName = new Date(year, month, day)
4. dateObjectName = new Date(year, month, day, hours, minutes, seconds)
dateObjectName is either the name of a new object or a property of an existing object.
To use Date methods:
dateObjectName.methodName(parameters)dateObjectName is either the name of an existing Date object or a property of an existing object..
Exceptions: The Date object's parse and UTC methods are static methods that you use as follows:
Date.UTC(parameters) Date.parse(parameters)
Form 1 of the syntax creates today's date and time. If you omit hours, minutes, or seconds from form 2 or 4 of the syntax, the value will be set to zero.
The way JavaScript handles dates is very similar to the way Java handles dates: both languages have many of the same date methods, and both store dates internally as the number of milliseconds since January 1, 1970 00:00:00.
JavaScript does not have a date data type, but you can use the Date object and its methods to work with dates and times in your applications. The Date object has many methods for setting, getting, and manipulating dates.
None.
None. Built-in objects do not have event handlers.
Contains information on the current document.
To define a document object, use standard HTML syntax:
<BODY BACKGROUND="backgroundImage" BGCOLOR="backgroundColor" FGCOLOR="foregroundColor" LINK="unfollowedLinkColor" ALINK="activatedLinkColor" VLINK="followedLinkColor" [onLoad="handlerText"] [onUnload="handlerText"]> </BODY>BACKGROUND specifies an image that fills the background of the document.
To use a document object's properties and methods:
1. document.propertyName 2. document.methodName(parameters)propertyName is one of the properties listed below.
The <BODY>...</BODY> tag encloses an entire document, which is defined by the current URL. The entire body of the document (all other HTML elements for the document) goes within the <BODY>...</BODY> tag.
You can reference the anchors, forms, and links of a document by using the anchors, forms, and links properties. These properties are arrays that contain an entry for each anchor, form, or link in a document.
None. The onLoad and onUnload event handlers are specified in the <BODY> tag but are actually event handlers for the window object.
The following example creates two frames, each with one document. The document in the first frame contains links to anchors in the document of the second frame. Each document defines its colors.
DOC1.HTML, which defines the frames, contains the following code:
DOC2.HTML, which defines the content for the first frame, contains the following code:
Some links
DOC3.HTML, which defines the content for the second frame, contains the following code:
Lets users input text and make choices from form objects such as checkboxes, radio buttons, and selection lists. You can also use a form to post data to or retrieve data from a server.
To define a form, use standard HTML syntax with the addition of the onSubmit event handler:
<FORM NAME="formName" TARGET="windowName" ACTION="serverURL" METHOD=GET | POST ENCTYPE="encodingType" [onSubmit="handlerText"]> </FORM>
NAME="formName" specifies the name of the form object.
TARGET="windowName" specifies the window that form responses go to. When you submit a form with a TARGET attribute, server responses are displayed in the specified window instead of the window that contains the form. windowName can be an existing window; it can be a frame name specified in a <FRAMESET> tag; or it can be one of the literal frame names _top, _parent, _self, or _blank; it cannot be a JavaScript expression (for example, it cannot be parent.frameName or windowName.frameName). Some values for this attribute may require specific values for other attributes. See RFC 1867 for details. You can access this value using the target property.
ACTION="serverURL" specifies the URL of the server to which form field input information is sent. Some values for this attribute may require specific values for other attributes. See RFC 1867 for details. You can access this value using the action property.
METHOD=GET | POST specifies how information is sent to the server specified by ACTION. GET (the default) appends the input information to the URL which on most receiving systems becomes the value of the environment variable QUERY_STRING. POST sends the input information in a data body which is available on stdin with the data length set in the environment variable CONTENT_LENGTH. Some values for this attribute may require specific values for other attributes. See RFC 1867 for details. You can access this value using the method property.
ENCTYPE="encodingType" specifies the MIME encoding of the form: "application/x-www-form-urlencoded" or "multipart/form-data". Some values for this attribute may require specific values for other attributes. See RFC 1867 for details. You can access this value using the encoding property.
To use a form object's properties and methods:
1. formName.propertyName 2. formName.methodName(parameters) 3. forms[index].propertyName 4. forms[index].methodName(parameters)formName is the value of the NAME attribute of a form object.
Each form in a document is a distinct object.
You can reference the form objects in your code by using the form name or the forms property of the document object. The forms property is an array that contains an entry for each form in a document.
You can reference a form's elements in your code by using the element's name (from the NAME="" attribute) or the elements property. The elements property is an array that contains an entry for each element (such as a checkbox, radio, or text object) in a form.
xxx to be supplied
A window that can display multiple, independently scrollable frames on a single screen, each with its own distinct URL. Frames can point to different URLs and be targeted by other URLs, all within the same screen. A series of frames makes up a page.
To define a frame object, use standard HTML syntax. The onLoad and onUnload event handlers are specified in the <FRAMESET> tag but are actually event handlers for the window object:
<FRAMESET ROWS="rowHeightList" COLS="columnWidthList" [onLoad="handlerText"] [onUnload="handlerText"]> [<FRAME SRC="locationOrURL" NAME="frameName">] </FRAMESET>ROWS="rowHeightList" is a comma-separated list of values specifying the row-height of the frame. An optional suffix defines the units. Default units are pixels.
To use a frame object's methods:
1. [windowReference.]frameName.methodName(parameters) 2. [windowReference.]frames[index].methodName(parameters)windowReference is a variable windowVar from a window definition (see window object), or one of the synonyms top or parent.
The <FRAMESET> tag is used in an HTML document whose sole purpose is to define the layout of frames that make up a page. Each frame is a window object.
You can reference the frame objects in your code by using the frames property of the window object. The frames property is an array that contains an entry for each frame in a window containing a <FRAMESET> tag.
If a <FRAME> tag contains SRC and NAME attributes, you can refer to that frame from a sibling frame by using parent.frameName or parent.frames[index]. For example, if the fourth frame in a set has NAME="homeFrame", sibling frames can refer to that frame using parent.homeFrame or parent.frames[3].
None. The onLoad and onUnload event handlers are specified in the <FRAMESET> tag but are actually event handlers for the window object.
The following example creates two windows, each with four frames. In the first window, the first frame contains pushbuttons that change the background colors of the frames in both windows.
FRAMSET1.HTML, which defines the frames for the first window, contains the following code:
FRAMSET2.HTML, which defines the frames for the second window, contains the following code:
FRAMCON1.HTML, which defines the content for the first frame in the first window, contains the following code:
Click here to load a different file into frame 2.
FRAMCON2.HTML, which defines the content for the remaining frames, contains the following code:
This is a frame.
FRAMCON3.HTML, which is referenced in a link object in FRAMCON1.HTML, contains the following code:
This is a frame. What do you think?
A text object that is suppressed from form display on an HTML form.
To define a hidden object:
<INPUT TYPE="hidden" NAME="hiddenName" [VALUE="textValue"]>NAME="hiddenName" specifies the name of the hidden object. You can access this value using the name property.
To use a hidden object's properties:
1. hiddenName.propertyName 2. formName.elements[index].propertyNamehiddenName is the value of the NAME attribute of a hidden object.
A hidden object is a form element and must be defined within a <FORM>...</FORM> tag.
You can use hidden objects instead of cookies for client/server communication.
None.
None.
Contains information on the URLs that the client has visited within a window. This information is stored in a history list, and is accessible through the Navigator's Go menu.
To use a history object:
1. history.propertyName 2. history.methodName(parameters)propertyName is one of the properties listed below.
The history object is a linked list of URLs the user has visited, as shown in the Navigator's Go menu.
None.
The following example goes to the URL the user visited three clicks ago in the current window.
You can use the history object with a specific window or frame. The following example causes window2 to go back one item in its window (or session) history:
The following example causes the second frame in a frameset to go back one item:
The following example causes the frame named frame1 in a frameset to go back one item:
The following example causes the frame named frame2 in window2 to go back one item:
A piece of text identified as a hypertext link. When the user clicks the link text, the link hypertext reference is loaded into its target window.
To define a link, use standard HTML syntax with the addition of the onClick and onMouseOver event handlers:
<A [NAME="anchorName"] HREF=locationOrURL TARGET="windowName" [onClick="handlerText"] [onMouseOver="handlerText"]> linkText </A>NAME="anchorName" specifies a tag that becomes an available hypertext target within the current document.
To use a link object's properties:
document.links[index].propertyNameindex is an integer representing a link object.
Each link object is a location object.
You can reference the link objects in your code by using the links property of the document object. The links property is an array that contains an entry for each link in a document.
When you specify a URL, you can use JavaScript statements in addition to using standard URL formats. The following list shows the syntax for specifying some of the most common types of URLs.
| URL type | Protocol | Example |
|---|---|---|
| JavaScript code | javascript: | javascript:history.go(-1) |
| World Wide Web | http:// | http://www.netscape.com/ |
| File | file:// | file:///javascript/methods.html |
| FTP | ftp:// | ftp://ftp.mine.com/home/mine |
| MailTo | mailto: | mailto:info@netscape.com |
| Gopher | gopher:// | gopher.myhost.com |
None.
The following example creates a hypertext link to an anchor named javascript_intro.
The following example creates a hypertext link to an anchor named numbers in the file DOC3.HTML in the window window2. If window2 does not exist, it is created.
The following example creates a hypertext link to a URL.
Contains information on the current URL.
To use a location object:
[windowReference.]location.propertyNamewindowReference is a variable windowVar from a window definition (see window object), or one of the synonyms top or parent.
The location object represents a complete URL. Each property of the location object represents a different portion of the URL.
The following diagram of a URL shows the relationships between the location properties:
protocol//hostname:port pathname search hash
protocol represents the beginning of the URL, up to and including the first colon.
hostname represents the domain name or IP address of a network host.
port represents the port number to connect to.
pathname represents the url-path portion of the URL.
search represents any query information in the URL, beginning with a question mark.
hash represents an anchor name fragment in the URL, beginning with a hash mark (#).
The location object has two other properties not shown in the diagram above:
href represents a complete URL.
host represents the concatenation hostname:port.
The location object is contained by the window object and is within its scope. If you reference a location object without specifying a window, the location object represents the current location. If you reference a location object and specify a window name, for example, windowReference.location.propertyName, the location object represents the location of the specified window.
Do not confuse the location object with the location property of the document object. You cannot change the value of the location property (document.location), but you can change the value of the location object's properties (window.location.propertyName). document.location is a string-valued property that usually matches what window.location is set to when you load the document, but redirection may change it.
None.
None.
The following example sets the URL of the current window to the Netscape home page:
The following example sets the URL of a frame named frame2 to the Sun home page:
See also the example for the anchors property.
A built-in object that has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi.
To use a Math object:
1. Math.propertyName 2. Math.methodName(parameters)propertyName is one of the properties listed below.
You reference the constant PI as Math.PI. Constants are defined with the full precision of real numbers in JavaScript.
Similarly, you reference Math functions as methods. For example, the sine function is Math.sin(argument), where argument is the argument.
It is often convenient to use the with statement when a section of code uses several Math constants and methods, so you don't have to type "Math" repeatedly. For example,
with (Math) {
a = PI * r*r
y = r*sin(theta)
x = r*cos(theta)
}
None. Built-in objects do not have event handlers.
See the examples for the individual properties and methods.
Contains information about the version of Navigator in use.
To use a navigator object:
navigator.propertyNamepropertyName is one of the properties listed below.
Use the navigator object to determine which version of the Navigator your users have.
None.
See the examples for the individual properties.
A text field on an HTML form that conceals its value by displaying asterisks (*). When the user enters text into the field, asterisks (*) hide anything entered from view.
To define a password object, use standard HTML syntax:
<INPUT TYPE="password" NAME="passwordName" [VALUE="textValue"] SIZE=integer>NAME="passwordName" specifies the name of the password object. You can access this value using the name property.
To use a password object's properties and methods:
1. passwordName.propertyName 2. passwordName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)passwordName is the value of the NAME attribute of a password object.
A password object is a form element and must be defined within a <FORM>...</FORM> tag.
None.
A set of radio buttons on an HTML form. A set of radio buttons lets the user choose one item from a list.
To define a set of radio buttons, use standard HTML syntax with the addition of the onClick event handler:
<INPUT TYPE="radio" NAME="radioName" VALUE="buttonValue" [CHECKED] [onClick="handlerText"]> textToDisplayNAME="radioName" specifies the name of the radio object. All radio buttons in a group have the same NAME attribute. You can access this value using the name property.
To use a radio button's properties and methods:
1. radioName[index1].propertyName 2. radioName[index1].methodName(parameters) 3. formName.elements[index2].propertyName 4. formName.elements[index2].methodName(parameters)radioName is the value of the NAME attribute of a radio object.
A radio object is a form element and must be defined within a <FORM>...</FORM> tag.
All radio buttons in a radio button group use the same name property. To access the individual radio buttons in your code, follow the object name with an index starting from zero, one for each button the same way you would for an array such as forms: document.forms[0].radioName[0] is the first, document.forms[0].radioName[1] is the second, etc.
The following example defines a radio button group to choose among three music catalogs. Each radio button is given the same name, NAME="musicChoice", forming a group of buttons for which only one choice can be selected. The example also defines 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 onClick event handler sets the catalog name input field when the user clicks a radio button.
A reset button on an HTML form. A reset button resets all elements in a form to their defaults.
To define a reset button, use standard HTML syntax with the addition of the onClick event handler:
<INPUT TYPE="reset" NAME="resetName" VALUE="buttonText" [onClick="handlerText"]>NAME="resetName" specifies the name of the reset object. You can access this value using the name property.
To use a reset object's properties and methods:
1. resetName.propertyName 2. resetName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)resetName is the value of the NAME attribute of a reset object.
A reset object is a form element and must be defined within a <FORM>...</FORM> tag.
The following example displays a text object with the default value "CA". If the user types a different state abbreviation in the text object and then clicks the Clear Form button, the original value of "CA" is restored.
A selection list or scrolling list on an HTML form. A selection list lets the user choose one item from a list. A scrolling list lets the user choose one or more items from a list.
To define a select object, use standard HTML syntax with the addition of the onBlur, onChange, and onFocus event handlers:
<SELECT NAME="selectName" [SIZE="integer"] [MULTIPLE] [onBlur="handlerText"] [onChange="handlerText"] [onFocus="handlerText"]> <OPTION VALUE="optionValue" [SELECTED]> textToDisplay [ ... <OPTION> textToDisplay] </SELECT>NAME="selectName" specifies the name of the select object. You can access this value using the name property.
To use a select object's properties and methods:
1. selectName.propertyName 2. selectName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)selectName is the value of the NAME attribute of a select object.
To use an option's properties:
1. selectName.options[index1].propertyName 2. formName.elements[index2].options[index1].propertyNameselectName is the value of the NAME attribute of a select object.
A select object is a form element and must be defined within a <FORM>...</FORM> tag.
You can reference the options of a select object in your code by using the options property. The options property is an array that contains an entry for each option in a select object: selectName.options[0] is the first, selectName.options[1] is the second, etc. Each option has the properties listed below.
The select object has the following properties:
The options property has the following properties:
None.
The following example displays a selection list and a scrolling list.
Choose the music types for your free CDs:
The following example displays two selection lists that let the user choose a month and day. These selection lists are initialized to the current date. The user can change the month and day by using the selection lists or by choosing preset dates from radio buttons. Text fields on the form display the values of the select object's properties and indicate the date chosen and whether it is Cinco de Mayo.
A series of characters.
To use a string object:
1. stringName.propertyName 2. stringName.methodName(parameters)stringName is the name of a string variable.
A string can be represented as a literal enclosed by single or double quotes; for example, "Netscape" or 'Netscape'.
None. Built-in objects do not have event handlers.
The following statement creates a string variable.
A submit button on an HTML form. A submit button causes a form to be submitted.
To define a submit button, use standard HTML syntax with the addition of the onClick event handler:
<INPUT TYPE="submit" NAME="submitName" VALUE="buttonText" [onClick="handlerText"]>NAME="submitName" specifies the name of the submit object. You can access this value using the name property.
To use a submit object's properties and methods:
1. submitName.propertyName 2. submitName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)submitName is the value of the NAME attribute of a submit object.
A submit object is a form element and must be defined within a <FORM>...</FORM> tag.
Clicking a submit button submits a form to the URL specified by the form's action property. This action always loads a new page into the client; it may be the same as the current page, if the action so specifies or is not specified.
The following example creates a submit object called submit_button. The text "Done" is displayed on the face of the button.
A text input field on an HTML form. A text field lets the user enter a word, phrase, or series of numbers.
To define a text object, use standard HTML syntax with the addition of the onBlur, on Change, onFocus, and onSelect event handlers:
<INPUT TYPE="text" NAME="textName" VALUE="textValue" SIZE=integer [onBlur="handlerText"] [onChange="handlerText"] [onFocus="handlerText"] [onSelect="handlerText"]>NAME="textName" specifies the name of the text object. You can access this value using the name property.
To use a text object's properties and methods:
1. textName.propertyName 2. textName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)textName is the value of the NAME attribute of a text object.
A text object is a form element and must be defined within a <FORM>...</FORM> tag.
text objects can be updated (redrawn) dynamically by setting the value property (this.value).
A multiline input field on an HTML form. A textarea field lets the user enter words, phrases, or numbers.
To define a text area, use standard HTML syntax with the addition of the onBlur, onChange, onFocus, and onSelect event handlers:
<TEXTAREA NAME="textareaName" ROWS="integer" COLS="integer" [onBlur="handlerText"] [onChange="handlerText"] [onFocus="handlerText"] [onSelect="handlerText"]> textToDisplay </TEXTAREA>NAME="textareaName" specifies the name of the textarea object. You can access this value using the name property.
To use a textarea object's properties and methods:
1. textareaName.propertyName 2. textareaName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)textareaName is the value of the NAME attribute of a textarea object.
A textarea object is a form element and must be defined within a <FORM>...</FORM> tag.
textarea objects can be updated (redrawn) dynamically by setting the value property (this.value).
The top-level object for each document, location, and history object group.
To define a window, use the open method:
windowVar = window.open("URL", "windowName", ["windowFeatures"])
windowVar is the name of a new window. Use this variable when referring to a window's properties, methods, and containership.
To use a window object's properties and methods:
1. window.propertyName 2. window.methodName 3. self.propertyName 4. self.methodName 5. top.propertyName 6. top.methodName 7. parent.propertyName 8. parent.methodName 9. windowVar.propertyName 10. windowVar.methodName 11. propertyName 12. methodNamewindowVar is a variable referring to a window object. See the preceding syntax for defining a window.
To define an onLoad or onUnload event handler for a window object, use the <BODY> or <FRAMESET> tags:
<BODY ... [onLoad="handlerText"] [onUnload="handlerText"]> </BODY> <FRAMESET ROWS="rowHeightList" COLS="columnWidthList" [onLoad="handlerText"] [onUnload="handlerText"]> [<FRAME SRC="locationOrURL" NAME="frameName">] </FRAMESET>
For information on the <BODY> and <FRAMESET> tags, see the document and frame objects.
The window object is the top-level object in the JavaScript client hierarchy. Frame objects are also windows.
The self and window properties are synonyms for the current window, and you can optionally use them to refer to the current window. For example, you can close the current window by calling either window.close() or self.close(). You can use these properties to make your code more readable, or to disambiguate the property reference self.status from a form called status. See the properties and methods listed below for more examples.
The top and parent properties are also synonyms that can be used in place of the window name. Top refers to the top-most Navigator window, and parent refers to a window containing a frameset. See the top and parent properties.
Because the existence of the current window is assumed, you do not have to reference the name of the window when you call its methods and assign its properties. For example, status="Jump to a new location" is a valid property assignment, and close() is a valid method call.
You can reference a window's frame objects in your code by using the frames property. The frames property is an array that contains an entry for each frame in a window containing a <FRAMESET> tag.
Windows lack event handlers until some HTML is loaded into them containing a <BODY> or <FRAMESET> tag.
In the following example, the document in the top window opens a second window, window2, and defines pushbuttons that open a message window, write to the message window, close the message window, and close window2. The onLoad and onUnload event handlers of the document loaded into window2 display alerts when the window opens and closes.
WIN1.HTML, which defines the frames for the first window, contains the following code:
WIN2.HTML, which defines the content for window2, contains the following code:
See also the example for the frame object.