The JavaScript Reference Pages

The following entries summarize the various JavaScript objects and independent functions. The properties, constants, arrays, methods, functions, and event handlers for each object are described in the entry for that object. Thus, if you want to read about the write() method of the Document object (Document.write), look it up in the entry for Document.

If you can't remember what object a method or property, etc., goes with, the following table should help. The left column lists the names of all the functions, properties, etc., and the right column gives the name of the object(s) with which they are associated. Since this table serves as something of a table of contents for this section, object names themselves also appear in the left hand column.

We've tried to cram as much useful information as possible into this chapter. But JavaScript has many intricacies to which we cannot do justice in so short a format. For more complete reference information, as well as an excellent guide to using the language, see JavaScript tutorial.

For See Object For See Object
abs( ) Math cookie Document
acos( ) Math cos( ) Math
action Form current History
alert( ) Window Date Date
alinkColor Document defaultChecked Checkbox
Anchor Anchor Radio
anchor( ) String defaultSelected Option
anchors[ ] Document defaultStatus Window
appCodeName Navigator defaultValue Text
applets[ ] Document Textarea
appName Navigator description MimeType
appVersion Navigator Plugin
arguments[ ] Function Document Document
Array Array document Window
asin( ) Math domain Document
assign( ) Object E Math
atan( ) Math Element Element
atan2( ) Math elements[ ] Form
back( ) History embeds[ ] Document
bgColor Document enabledPlugin MimeType
big( ) String encoding Form
blink( ) String escape( ) escape( )
blur( ) FileUpload eval( ) eval( )
Password Object
Text exp( ) Math
Textarea fgColor Document
Window FileUpload FileUpload
bold( ) String filename Plugin
Boolean Boolean fixed( ) String
border Image floor( ) Math
Button Button focus( ) FileUpload
caller Function Password
ceil( ) Math Text
charAt( ) String Textarea
Checkbox Checkbox Window
checked Checkbox fontcolor( ) String
Radio fontsize( ) String
clear( ) Document Form Form
clearTimeout( ) Window form Button
close( ) Document Checkbox
Window Element
closed Window FileUpload
complete Image Hidden
confirm( ) Window Password
form (cont'd) Radio length (cont'd) JavaArray
Reset Select
Select String
Submit Window
Text Link Link
Textarea link( ) String
forms[ ] Document linkColor Document
forward( ) History links[ ] Document
Frame Frame LN10 Math
frames[ ] Window LN2 Math
Function Function location Document
getClass( ) getClass( ) Location Location
getDate( ) Date location Window
getDay( ) Date log( ) Math
getHours( ) Date LOG10E Math
getMinutes( ) Date LOG2E Math
getMonth( ) Date lowsrc Image
getSeconds( ) Date Math Math
getTime( ) Date max( ) Math
getTimezoneOffset( ) Date MAX_VALUE Number
getYear( ) Date method Form
go( ) History MimeType MimeType
hash Location mimeTypes Navigator
height Image mimeTypes[ ] Plugin
Hidden Hidden min( ) Math
History History MIN_VALUE Number
history Window name Button
host Location Checkbox
hostname Location Element
href Location FileUpload
hspace Image Hidden
Image Image Image
images[ ] Document MimeType
index Option Password
indexOf( ) String Plugin
isNaN( ) isNaN( ) Radio
italics( ) String Reset
java java Select
Packages Submit
JavaArray JavaArray Text
JavaClass JavaClass Textarea
javaEnabled( ) Navigator Window
JavaObject JavaObject NaN Number
JavaPackage JavaPackage Navigator Navigator
join( ) Array navigator navigator
lastIndexOf( ) String NEGATIVE_INFINITY Number
lastModified Document netscape netscape
length Array Packages
History next History
Number Number previous History
Object Object prompt( ) Window
onabort( ) Image protocol Location
onblur( ) FileUpload prototype Function
Text Radio Radio
Textarea random( ) Math
Window referrer Document
onchange( ) FileUpload reload( ) Location
Select replace( ) Location
Text Reset Reset
Textarea reset( ) Form
onclick( ) Button reverse( ) Array
Checkbox round( ) Math
Link scroll( ) Window
Radio search Location
Reset Select Select
Submit select( ) Text
onerror( ) Image selected Option
Window selectedIndex Select
onfocus( ) FileUpload self Window
Text setDate( ) Date
Textarea setHours( ) Date
Window setMinutes( ) Date
onload( ) Image setMonth( ) Date
Window setSeconds( ) Date
onmouseout( ) Link setTime( ) Date
onmouseover( ) Link setTimeout( ) Window
onreset( ) Form setYear( ) Date
onsubmit( ) Form sin( ) Math
onunload( ) Window small( ) String
open( ) Document sort( ) Array
Window split( ) String
opener Window sqrt( ) Math
Option Option SQRT1_2 Math
options[ ] Select SQRT2 Math
Packages Packages src Image
parent Window status Window
parse( ) Date strike( ) String
parseFloat( ) parseFloat( ) String String
parseInt( ) parseInt( ) sub( ) String
Password Password Submit Submit
pathname Location submit( ) Form
PI Math substring( ) String
Plugin Plugin suffixes MimeType
plugins Document sun Packages
Navigator sun
port Location sup( ) String
POSITIVE_INFINITY Number taint( ) taint( )
pow( ) Math taintEnabled( ) Navigator
tan( ) Math UTC( ) Date
target Form value Button
Link Checkbox
text Option Element
Text Text FileUpload
Textarea Textarea Hidden
title Document Option
toGMTString( ) Date Password
toLocaleString( ) Date Radio
toLowerCase( ) String Reset
top Window Submit
toString( ) Boolean Text
Function Textarea
Number valueOf( ) Object
Object vlinkColor Document
toUpperCase( ) String vspace Image
type Element width Image
Select Window Window
unescape( ) unescape( ) window Window
untaint( ) untaint( ) write( ) Document
URL Document writeln( ) Document
userAgent Navigator

Anchor Object

Represents a named position (of an HTML document) that may be the target or destination of a hypertext link. A hypertext link may refer to an anchor by using its name after a # character in a URL. In Netscape 2.0, the elements of the document.anchor[] array are set to null, so it is not possible to actually obtain an Anchor object. See also Document.Link.

document.anchors.length // number of anchors in the document document.anchors[i] // one of the Anchor objects 

An Anchor object is created by any standard HTML <a> tag that contains a <name> attribute:

<a name="anchor_name" links may refer to this anchor by this name [ href=URL ] an anchor may also be a link [ target="window_name" ] links may refer to other windows > anchor HTML text </a> 

Array Object

Creates and initializes an array. Along with the usual array capabilities that all JavaScript objects have, the Array object provides additional array functionality: a constructor function for initializing arrays, an automatically updated length field that stores the size of the array, and join(), reverse(), and sort() methods that manipulate the elements of an array. Available in Netscape 3.0. See also Object.

new Array() with no arguments, length field is set to 0 new Array(size) size = number of elements; sets length new Array(element0, element1, ..., elementn) length set to number of elements 

Properties

Methods

Boolean Object

An object wrapper around the boolean value; exists solely to provide a toString() method to convert boolean values to strings. When the toString() method is invoked to convert a boolean value to a string (and it is often invoked implicitly by JavaScript), JavaScript internally converts the boolean value to a transient Boolean object, on which the method can be invoked.

You can create Boolean objects that are not transient by calling the Boolean() constructor method:

new Boolean(value) 

The argument is the value to be held by the Boolean object. This will be converted to a boolean value, if necessary. The values 0, null, and the empty string "" are all converted to false. All other values, including the string "false," are converted to true. Available in Netscape 3.0. See also Object.

Methods

Button Object

Represents a graphical pushbutton in a form within an HTML document. Use a Button object whenever you want to allow the user to trigger some action on your Web page. Note that the Submit and Reset objects are types of Button objects that submit a form and reset a form's values. Often these default actions are sufficient for a form, and you do not need to create any other types of buttons. Available in Netscape 2.0; enhanced in 3.0. See also Element, Form, Reset, Submit.

form.button_name form.elements[i] form.elements['button_name'] 

Properties

Event Handlers

HTML syntax

A Button object is created with a standard HTML <input> tag, with the addition of the onClick attribute:

<form> ... <input type="button" specifies that this is a button value="label" the text that is to appear within the button; specifies the value property [ name="name" ] a name that can later be used to refer to the button; specifies the name property [ ] JavaScript statements to be executed when the button is clicked > ... </form> 

Checkbox Object

Represents a single graphical checkbox in an HTML form. Note that the text that appears next to the checkbox is not part of the Checkbox object itself, and must be specified external to the Checkbox's HTML <input> tag. The onClick event handler allows you to specify JavaScript code to be executed when the Checkbox is checked or "un-checked." The value of the checked property gives the state of the Checkbox; it can also be set to change the state. Available but buggy in Netscape 2.0; enhanced in 3.0. See also Element, Form, Radio.

A Checkbox object with a unique name may be referenced in any of these ways:

form.checkbox_name form.elements[i] form.elements['checkbox_name'] 

When a form contains a group of checkboxes with the same name, they are placed in an array, and may be referenced as follows:

form.checkbox_name[j] form.checkbox_name.length form.elements[i][j] form.elements[i].length form.elements['checkbox_name'][j] form.elements['checkbox_name'].length 

Properties

Event handlers

HTML syntax

A Checkbox object is created with a standard HTML <input> tag, with the addition of the new onClick attribute. Multiple Checkbox objects are often created in groups by specifying multiple <input> tags which have the same name attribute.

<form> ... <input type="checkbox" specifies that this is a checkbox [ name="name" ] a name that can later be used to refer to this checkbox or to the group of checkboxes with this name; specifies the name property [ value="value" ] the value returned when this checkbox is selected; specifies the value property [ checked ] specifies that the checkbox is initially checked Specifies the defaultChecked property [ ] JavaScript statements to be executed when the Checkbox is clicked > label the HTML text that should appear next to the Checkbox ... </form> 

Date Object

With no arguments, the Date() method creates a Date object set to the current date and time. Otherwise, the arguments to Date() specify the date, and, optionally, the time, for the new object. The Date object is built into JavaScript and does not have an HTML analog. Most of the Date object methods are invoked through an instance of the Date object. For example:

d = new Date(); //get today's date and time system.write('Today is: " + d.toLocaleString()); //and print it out 

This syntax for creating Date objects assumes that date and time values are specified in local time. When your code must work the same way regardless of the time zone in which it is run, you should specify all your hard-coded dates in the GMT (or UTC) time zone. The most common use of the Data object is to subtract the millisecond representations of the current time from some other time to determine the difference.

Buggy to the point of uselessness in Netscape 2.0. See also Date.parse, Date.UTC(). (Note that the Date.parse() and Date.UTC() functions, though related to Date, do not operate on the Date object.)

To create a Date object, use one of the following five syntaxes. In the third through fifth syntaxes, the specified times are interpreted as local (not GMT) times.

new Date(); new Date(milliseconds) milliseconds between date and 12AM 1/1/70 new Date(date_string); date_string = month_name dd, yy [hh:mm[:ss]] new Date(year, month, day); year minus 1900; month 0-11; day 1-31 new Date(year, month, day, hours, minutes, seconds) 24-hour clock 

Methods

Date.parse( ) Method

Date.parse() is a function that is related to the Date object, but it is not a method of (or invoked on) the Date object. Date.parse() parses a date/time string and returns the number of milliseconds between the specified date/time and midnight, January 1st, 1970, GMT. This number can be used directly, used to create a new Date object, or to set the date in an existing Date object with Date.setTime().

Date.parse() understands the IETF standard date format used in email and other Internet communications (e.g., Wed, 8 May 1996 17:41:46 -0400), as well as partial dates of this format; it also understands the GMT time zone, and the standard abbreviations for the time zones of the U.S. Buggy in Netscape 2.0. See also Date, Date.UTC( ).

date.parse(date_string) 

Date.UTC( ) Method

Date.UTC() is a function that is related to the Date object, but is not a method of the Date object or invoked on it; it is always invoked as Date.UTC(), not as date.UTC(), on some object date.

Date.UTC converts time in UTC (Universal Coordinated Time) format (i.e., in the GMT zone) to milliseconds. It returns the number of milliseconds between midnight on January 1st, 1970, UTC and the time specified by the arguments. This can be used by the Date() constructor method and by the Date.setTime() method.

For arguments, use: year minus 1900 (e.g., 96 for 1996); month 0 (January) through 11 (December); 24-hour clock for hour (0-23). In Netscape 2.0, Date.UTC() does not compute the correct number of milliseconds. See also Date, Date.parse.

date.UTC(year, month, day, [, hours [, minutes [, seconds ]]]); 

To create a Date object using a UTC time specification, you can use code like this:

d = new Date(Date.UTC(96, 4, 8, 16, 30)); 

Document Object

The currently displayed HTML document. An instance of the Document object is stored in the document field of the Window object. As a special case, when referring to the Document object of the current window (i.e., the window in which the JavaScript code is executing), you can omit the window reference and simply use document. Available in Netscape 2.0. See also Form, Frame, Window.

window.document document // To refer to Document obj of current window 

Properties

Note that for all attributes to set a color, the value can be one of the standard color names recognized by JavaScript, or an RGB value in six hexadecimal digits (RRGGBB).

Methods

Event handlers

The following event handlers are, strictly speaking, properties of Window, not Document:

Element Object

Technically speaking, there is no single Element object in JavaScript. Each of the various types of form elements are types of Element objects. Available in Netscape 2.0. See also Button, Checkbox, FileUpload, Form, Hidden, Password, Radio, Reset, Select, Submit, Text, Textarea.

form.elements[i] form.name 

Properties

escape( ) Function

The built-in escape() function creates and returns a new string that contains an encoded version of s to allow transmission of data. A common use of escape() is to encode cookie values, which have restrictions on the punctuation characters they may contain. Available in Netscape 2.0. See also String, unescape( ).

escape(s) s is the string to be "escaped" or encoded 

All spaces, punctuation, and accented characters are converted to the form %xx, where xx is two hexadecimal digits that represent the ISO-8859-1 (Latin-1) encoding of the character. For example:

escape("Hello World!"); 

yields the string:

Hello%20World%21 

eval( ) Function

A built-in JavaScript function; not a method of any object. Executes the code in its string argument code, which may contain one or more JavaScript statements (separated by semicolons). You can also use eval() to evaluate a JavaScript expression rather than execute a statement. Returns the value of the last expression in code that it evaluates. eval() allows a JavaScript program to dynamically modify the code that it executes.

Crashes Netscape 2.0 on 16-bit Windows (version 3.1) platforms. A possible workaround: use Window.setTimeout( ) with a zero-millisecond delay. In 3.0, eval has become a method of the Object object. See also Object, Window.

eval(code) 

FileUpload Object

Represents a file upload input element in a form. It looks like a text input field, with the addition of a Browse... button that opens a directory browser. Entering a filename into a FileUpload object (either directly or through the browser) causes Netscape to submit the contents of that file along with the form (which must use "multipart/form-data" encoding and the post method). The FileUpload object does not recognize the HTML value attribute to specify an initial value for the input field. For security reasons, only the user may enter a filename; JavaScript may not enter text into the FileUpload field in any way. Available in Netscape 2.0; enhanced in 3.0. See also Element, Form, Text.

form.name form.elements[i] form.elements['name'] 

Properties

Methods

Event handlers

HTML syntax

A FileUpload object is created with a standard HTML <input> tag, with the addition of optional attributes for event-handlers:

<form ENCtype="multipart/form-data" method=post> required attributes ... <input type="file" specifies that this is a FileUpload object [ name="name" ] a name that can later be used to refer to this object specifies the name property [ size=integer ] how many characters wide the object is [ maxlength=integer ] max allowed number of input characters [ onBlur="handler" ] the onblur() event handler [ onChange="handler" ] the onchange() event handler [ onFocus="handler" ] the onfocus() event handler ... 

Form Object

Represents an HTML <form> in a document. Each form in a document is represented as an element of the Document.forms[] array. Named forms are also represented by the form_name property of their document, where form_name is the name specified in the name attribute of the <form> tag. Available in Netscape 2.0. See also Button, Checkbox, Element, FileUpload, Hidden, Password, Radio, Reset, Select, Submit, Text, Textarea.

document.form_name document.forms[form_number] document.forms.length 

The elements of a form (buttons, input fields, check boxes, and so on) are collected in the Form.elements[] array. Named elements, like named forms, can also be referenced directly by name--the element name is used as a property name of the Form object. Thus, to refer to a Text object element named "phone" within a form named "questionnaire," you might use the JavaScript expression:

document.questionnaire.phone 

Properties

Methods

Event Handlers

HTML syntax

A Form object is created with a standard HTML <form> tag. JavaScript adds the optional onSubmit event handler attribute to this tag. The form contains any input elements created with the <input> tag between <form> and </form>.

<form [ name="form_name" ] to name the form in JavaScript [ target="window_name" ] the name of the window for responses [ action="url" ] the URL to which the form is submitted [ method=(get|post) ] the method of form submission [ enctype="encoding" ] how the form data is encoded [ onSubmit="handler" ] a handler invoked when form is submitted > form text and <input> tags go here </form> 

Frame Object

Though the Frame object is sometimes referred to, there is, strictly speaking, no such object. All frames within a browser window are instances of the Window object, and they contain the same properties and support the same methods, and event handlers as the Window object does. See the Window object, and its properties, methods, and event handlers for details.

Note, however, that there are a few practical differences between Window objects that represent top-level browser windows and those that represent frames within a browser window:

Available in Netscape 2.0.

window.frames[i] window.frames.length frames[i] frames.length 

Function Object

An object wrapper around the basic function data type; this object type exists so that functions can have properties and methods associated with them. When a function value is used in an "object context," i.e., when you attempt to invoke a method or read a property of a function, JavaScript internally converts the function value into a temporary Function object, so that the method can be invoked or the property value read.

function functionname(argname1 [, . . . argname_n)] {
 body // body of function
}

To create a new function, use the Function() constructor method:

new Function([argname1 [, ..., argname_n]], body) 

Functions defined in this way are sometimes called "anonymous" because they are not given a name when they are created. Just as JavaScript converts from a function value to a Function object whenever necessary, so it will convert from a Function object (created with the Function() constructor) to a function value whenever you use the object in a function value context--i.e., whenever you invoke it with the () operator. This conversion from Function object to function value is done by the valueOf( ) method.

Since there is no special keyword in JavaScript that refers to the Function object of the currently executing function, you must refer to Function objects by name, as in:

function myfunc() {
 if (myfunc.arguments.length == 0) return; ...
}

Enhanced in Netscape 3.0. See also the Object Object.

Properties

Methods

getClass( ) Function

A function that takes a JavaObject object and returns the JavaClass object of that JavaObject. Available in Netscape 3.0. See also JavaArray, JavaClass, JavaObject, JavaPackage, and Packages.

getClass(javaobj) 

Usage

Don't confuse the JavaScript getClass() function with the getClass method of all Java objects. Similarly, don't confuse the JavaScript JavaClass object with the Java java.lang.Class class.

Consider the Java rectangle object created with the following line:

var r = new java.awt.Rectangle(); 

r is a JavaScript variable that holds a JavaObject object. Calling the JavaScript function getClass() returns a JavaClass object that represents the java.awt.Rectangle class:

var c = getClass(r); 

You can see that this is so by comparing this JavaClass object to java.awt.Rectangle:

if (c == java.awt.Rectangle) ... 

The Java getClass() method is invoked differently and performs an entirely different function:

c = r.getClass(); 

After executing the above line of code, c is a JavaObject that represents a java.lang.Class object. This java.lang.Class object is a Java object that is a Java representation of the java.awt.Rectangle class. See your Java documentation for details on what you can do with the java.lang.Class class.

To summarize, you can see that the following expression will always evaluate to true for any JavaObject o:

(getClass(o.getClass()) == java.lang.Class) 

Hidden Object

An invisible form element that allows arbitrary data to be transmitted to the server when the form is submitted. You can use a Hidden object when you want to transmit additional information, besides the user's input data, to the server. (Cookies can also be used to transmit data from client-to-server; however, cookies are persistent on the client side.)

When an HTML document is generated on the fly by a server, another use of Hidden form elements is to transmit data from the server to the client for later processing by JavaScript on the user's side.

Hidden objects can also be useful for communication between CGI scripts, even without the intervention of JavaScript on the client side. In this usage, one CGI script generates a dynamic HTML page containing hidden data, which is then submitted back to a second CGI script. This hidden data can communicate state information, such as the results of submission of a previous form. Enhanced in Netscape 3.0. See also Element, Form, Document.

form.name form.elements[i] form.elements['name'] 

Properties

HTML syntax

A Hidden object is created with a standard HTML <input> tag:

<form> ... <input type="hidden" specifies that this is a Hidden object [ name="name" ] a name that can later be used to refer to this object; specifies the name property [ value="value" ] the value transmitted when the form is submitted; specifies the initial value of the value property > ... </form> 

History Object

Read-only array of strings that specify the URLs that have been previously visited by the browser. The contents of this list are equivalent to the URLs listed in Netscape's Go menu. You can use the History object to implement your own Forward and Back buttons, or other navigation controls, within a window.

In Netscape 2.0, and in 3.0 without data tainting, JavaScript can use the length property to determine the number of entries on the History object's URL list, and can use the back(), forward(), and go() methods to cause the browser to revisit any of the URLs on the list, but it cannot directly or indirectly read the URLs stored in the array.

In 3.0 and later, when the data tainting security model is enabled, the elements of the array are available and may be read (but not changed). Additional properties (described below) are also available. See also Location.

window.history frame.history history 

Properties

Methods

Image Object

The Image objects in the document.images[] array represent the images embedded in an HTML document using the <img> tag. Only two properties are writeable: src and lowsrc. When you set src, the browser will load the image specified by the new value of the src property, or by the lowsrc property, for low-resolution monitors. (Note that lowsrc must be set before src because the latter starts the download of the new image.) Setting src can be used to change the graphics on a page in response to user actions (e.g., changing the image on a button to indicate that it is or is not available for selection based on whether the user has input certain information).

Available in Netscape 3.0. (Note that because of a bug in Netscape 2.0, all images in a page that contains JavaScript must have width and height attributes specified, even though the Image object is not available in 2.0.) See also Document.

document.images[i] document.images.length document.image-name 

You can dynamically create Image objects using the Image() constructor method:

new Image([width, height]); 

Properties

Event handlers