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: The Definitive Guide.
| 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 |
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>
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
Read/write integer specifying the number of elements in the array, or, when the array does not have contiguous elements, a number one larger than the index of the last element in the array. The length property of a new array is initialized when the array is created with the Array() constructor method. Adding new elements to an array created with the Array() constructor updates the length, if necessary:
a = new Array(); a.length initialized to 0
b = new Array(10); b.length initialized to 10
c = new Array("one", "two", "three"); c.length initialized to 3
c[3] = "four"; c.length updated to 4
c[10] = "blastoff"; c.length becomes 11
You can also set the value of the length property to change the size of an array (i.e., truncate elements or add "undefined" ones).
Converts each of the elements of an array to a string, and then concatenates those strings, inserting the specified separator string between the elements. Returns the resulting string. You can split a string up into array elements--with the split() method of the String object.
array.join() array.join(separator) if no separator, the empty string is used
Reverse, in place (i.e., without creating a new array), the order of the elements of an array.
With no arguments, sorts alphabetically (by character encoding); elements are first converted to strings, if necessary, so that they can be compared. To sort the array elements in some other order, you must supply a function that compares two values and returns a number indicating their relative order.
array.sort() array.sort(orderfunc) orderfunc - optional comparison function
The comparison function should take two arguments, a and b, and should:
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.
Returns true or false, depending on the boolean value represented by the Boolean object.
Returns the boolean value represented by the Boolean 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']
Read-only reference to the Form object that contains the specified button object.
Set by the name attribute of the HTML <input> tag that creates the button, this read-only string property provides the name of the button.
form.button_name form.elements['button_name']
See the type property of the Element object (Element.type).
Set by the value attribute of the HTML <input> tag that creates the button, this read-only string property provides text displayed in the Button object.
Invoked when the button is clicked; defined by the onClick attribute of the HTML <input> tag. Value may be any number of JavaScript statements, separated by semicolons, that are executed when the user clicks the button.
<INPUT TYPE="button" a definition of the handler
value="button-text"
onClick="handler-statements">
button.onclick a reference to the handler
button.onclick() an explicit invocation of the handler
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
[ onClick="handler" ] JavaScript statements to be executed
when the button is clicked
>
...
</form>
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
Read/write Boolean property that specifies whether the Checkbox is checked (true) or not (false). Setting the checked property changes the appearance of the Checkbox but does not cause the onclick() event handler to be invoked.
Read-only Boolean property that represents the Checkbox's initial state. May be specified using the checked attribute in the HTML <input> tag. Can be used to reset a Checkbox to its default state.
Read-only reference to the Form object that contains the Checkbox.
Read-only string, set by the HTML name attribute, that specifies the name of the Checkbox object (or array of Checkbox objects).
See the type property of the Element object (Element.type).
Read/write string that specifies the text that is passed to the Web server if the Checkbox is checked when the form is submitted. The initial value of value is specified by the HTML value attribute. If no value attribute is specified, then the default value string is "on."
Invoked when the user clicks on a Checkbox; it is defined by the HTML onClick attribute. The value of this attribute may be any number of JavaScript statements, separated by semicolons, which are executed when the user clicks on the Checkbox.
<INPUT type="checkbox" a definition of the handler
onClick="handler-statements">
checkbox.onclick a reference to the handler
checkbox.onclick(); an explicit invocation of the handler
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
[ onClick="handler" ] JavaScript statements to be executed when the
Checkbox is clicked
>
label the HTML text that should appear next to the Checkbox
...
</form>
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
Returns the day of the month of a Date object. Return values are between 1 and 31.
Returns the day of the week of a Date object. Return values are between 0 (Sunday) and 6 (Saturday).
Returns the hours field of a Date object. Return values are between 0 (midnight) and 23 (11 PM).
Returns the minutes field of a Date object. Return values are between 0 and 59.
Returns the month field of a Date object. Return values are between 0 (January) and 11 (December).
Returns the seconds field of a Date object. Return values are between 0 and 59.
Returns the internal, millisecond representation of a Date object (i.e., the number of milliseconds between midnight GMT on 1/1/1970 and the specified date).
Returns the difference in minutes between this date (in the local time zone) and GMT. Tells you what time zone the JavaScript code is running in. Since getTimezoneOffset is invoked through a Date object, but doesn't reference the Date object, it should actually be its own function.
Returns the year field of a Date object. Return value is the year minus 1900 (e.g., 96 for 1996).
Parses a string representation of a date and returns it in millisecond format.
Sets the day of the month field of a Date object.
date.setDate(day_of_month) // day_of_month is 1-31
Sets the hour field of a Date object.
date.setHours(hours) //hours is integer betw 0 (midnight) and 23 (11pm)
Sets the minutes field of a Date object.
date.setMinutes(minutes) //minutes is integer betw 0 and 59
Sets the month field of a Date object.
date.setMonth(month) //month is integer betw 0 (Jan) and 11 (Dec)
Sets the seconds field of a Date object.
date.setSeconds(seconds) //seconds is integer betw 0 and 59
Sets a Date object in the milliseconds between the desired date/time and midnight GMT on January 1, 1970. Representing a date in this millisecond format makes it independent of time zone.
date.setTime(milliseconds)
Sets the year field of a Date object.
date.setYear(year) // year is year minus 1900; e.g. 96 for 1996
Converts a Date to a string, using the GMT time zone; format of string varies slightly according to platform.
Converts a Date to a string, using the local time zone; uses local conventions for data and time formatting.
Converts a numeric date and time specification to millisecond format.
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() 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));
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
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).
String that specifies the color of activated links (i.e., links being selected by user). Can be set directly in the document <head>, or inherited from the alink attribute in the <body>.
An array of Anchor objects, one for each anchor (i.e., hypertext target) in the document.
Read-only integer specifying the number of elements in the anchors[] array.
An array of Java objects, one for each <applet> that appears in the document.
A read-only integer specifying the number of elements in the applets[] array.
String that specifies the background color of the document. Can be set directly at any point in document, or inherited from the bgcolor attribute in the <body>. Buggy in Netscape 2.0.
A string that is the value of a cookie associated with this document. String property that allows you to read, create, modify, and delete the cookie(s) that apply to the current document. A "cookie" is a small amount of named data stored by the Web browser so that it can use data input on one page in another page, or recall user preferences across Web-browsing sessions.
document.cookie
The read and write values of the cookie property generally differ. In a JavaScript expression, the cookie property returns a string containing all the cookies from the current document, in name=value pairs (separated by semicolons). Use String.indexOf() and String.substring() to determine the value of a particular cookie. Since cookies cannot contain any semicolons, commas, or whitespace, they are commonly encoded using escape() before storing and decoded using unescape() after retrieving.
To associate a cookie value with the current document for the current Web browsing session, set document.cookie to a string of the form name=value. To create a cookie that can last across browser sessions, include an expiration date by setting document.cookie to a string of the form:
name=value; expires=date
date should be a date specification in the format written by Date.toGMTString().
String that specifies the Internet domain which the document is from; used for security purposes.
An array of Java objects, one for each <embed> tag that appears in the document.
Read-only integer that specifies the number of elements in the embeds[] array.
String that specifies the default color of document text. Can be set directly in the document <head>, or inherited from the text attribute in the <body>.
An array of Form objects, one for each <form> that appears in the document.
Read-only integer specifying the number of elements in the forms[] array.
An array of Image objects, one for each image embedded in the document with the <img> tag.
The number of elements in the images[] array.
Read-only string that contains the (local) date and time at which document was most recently modified (derived from the HTTP header).
String that specifies the color of unvisited links. Can be set directly in the document <head>, or inherited from the link attribute in the <body>.
An array of Link objects, one for each hypertext link in the document.
Read-only integer specifying the number of elements in the links[] array.
Synonym for the URL property. Use URL instead because it is less likely to be confused with the Window.location property.
Synonym for the embeds[] array.
The number of elements in the plugins[] or embeds[] array.
Read-only string that contains the URL of the document from which the current document was reached.
Read-only string that specifies the <title> of the document.
Read-only string that specifies the URL of the document that contained the link that referred to the current document.
String that specifies the color of visited links. Can be set directly in the document <head>, or inherited from the vlink attribute in the <body>.
Clears the window or frame that contains document.
Displays any output to document that has been written but not yet displayed, and closes the output stream.
Opens a stream to document, so that subsequent document.write() calls can append data to the document.