21.3 The JavaScript Reference PagesThe 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.
Anchor ObjectRepresents 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 ObjectCreates 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 ObjectAn 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 ObjectRepresents 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 syntaxA 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> Checkbox ObjectRepresents 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 syntaxA 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> Date ObjectWith 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( ) MethodDate.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( ) MethodDate.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 ObjectThe 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 PropertiesNote 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 handlersThe following event handlers are, strictly speaking, properties of Window, not Document:
Element ObjectTechnically 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( ) FunctionThe 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( ) FunctionA 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 ObjectRepresents 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 syntaxA 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 ObjectRepresents 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 syntaxA 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 ObjectThough 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 ObjectAn 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( ) FunctionA 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) UsageDon'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 ObjectAn 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 syntaxA 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 ObjectRead-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 ObjectThe 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
HTML syntaxThe Image object is created with a standard HTML <img> tag, with the addition of event handlers. Some <img> attributes have been omitted from the syntax below, because they are not used by or accessible from JavaScript.
<img src="url" the image to display width=pixels the width of the image height=pixels the height of the image [ name="image-name" ] a property name for the image [ lowsrc="url" ] alternate low-resolution image [ border=pixels ] width of image border [ hspace=pixels ] extra horizontal space around image [ vspace=pixels ] extra vertical space around image [ onLoad=handler ] invoked when image is fully loaded [ onError=handler ] invoked if error in loading [ onAbort=handler ] invoked if user aborts load > isNaN( ) FunctionTests whether an argument (x) is "not a number"; specifically determines whether it is the reserved value NaN, which represents an illegal number (such as the result of dividing zero by zero). This function is required because it is not possible to express the NaN value as a literal in JavaScript. Commonly used to test the results of parseFloat() and parseInt() to see if they represent legal numbers, or to check for arithmetic errors, such as division by zero. Not implemented on all platforms for Netscape 2.0. See also parseFloat, parseInt.
isNaN(x) JavaArray ObjectA representation of a Java array, which allows JavaScript to read and write the elements of the array using familiar JavaScript array syntax. When reading and writing values from array elements, data conversion between JavaScript and Java representations is handled by the system. Note that Java arrays differ from JavaScript arrays in the following ways. First, Java arrays have a fixed length that is specified when they are created; thus, the JavaArray length field is read-only. Second, Java arrays are typed (i.e., their elements must all be of the same data type); attempting to set an array element to a value of the wrong type will result in a JavaScript error. Available in Netscape 3.0. See also getClass, JavaClass, JavaObject, JavaPackage, Packages.
javaarray.length the length of the array javaarray[index] read or write an array element Properties
UsageIf java.awt.Polygon is a JavaClass object, you can create a JavaObject representing an instance of the class using:
p = new java.awt.Polygon(); This object p has properties xpoints and ypoints, which are JavaArray objects representing Java arrays of integers. You could initialize the contents of these arrays with JavaScript code like the following:
for(int i = 0; i < p.xpoints.length; i++) p.xpoints[i] = Math.round(Math.random()*100); for(int i = 0; i < p.ypoints.length; i++) p.ypoints[i] = Math.round(Math.random()*100); JavaClass ObjectJavaScript representation of a Java class. Its properties represent the public static fields and methods (also called class fields and methods) of the represented class; these properties can be used to read and write the static fields and to invoke the static methods of Java classes. Use a for/in loop to enumerate the properties for any given class. Note that the JavaClass object does not have properties representing the instance fields of a Java class, which are represented by the JavaObject object. However, the JavaClass object does allow for the creation of Java objects (represented by a JavaObject object) using the new keyword and invoking the constructor method of a JavaClass. For primitive data types, conversion between JavaScript values and Java values is handled automatically by the system. Note that Java is a typed language (i.e., each of the fields of an object must adhere to a specific data type). Available in Netscape 3.0. See also getClass, JavaArray, JavaObject, JavaPackage, Packages.
javaclass.static_field read or write a static Java field javaclass.static_method(...) invoke a static method new javaclass(...) create a new Java object Usagejava.lang.System is a JavaClass object that represents the java.lang.System class in Java. The following code reads a static field of this class:
var java_console = java.lang.System.out; Invoke a static method of this class with a line such as:
var version = java.lang.System.getProperty("java.version"); The JavaClass object allows you to create a new Java object like this:
var java_date = new java.lang.Date(); JavaObject ObjectJavaScript representation of a Java object. Its properties represent the public instance fields and methods defined for the Java object; these properties can be used to read and write the public instance fields and to invoke the public instance methods of a Java object. (The static/class fields and methods are represented by the JavaClass object.) Use the for/in loop to enumerate the properties of any given JavaObject. For primitive data types, conversion between JavaScript values and Java values is handled automatically by the system. Note that Java is a typed language (i.e., each of the fields of an object must adhere to a specific data type). Available in Netscape 3.0. See also getClass, JavaArray, JavaClass, JavaPackage, Packages.
javaobject.field read or write an instance field javaobject.method(...) invoke an instance method Usagejava.lang is the name of a JavaPackage that contains the JavaClass java.lang.System. This class has the property out, which is a JavaObject. This JavaObject has a property println, which is a method that can be invoked like this:
java.lang.System.out.println("Hello from Java!"); The previous line of code will write a message on the Java console. java.awt.Rectangle is a JavaClass that represents the java.awt.Rectangle class. The following line creates a JavaObject that represents an instance of this class:
var r = new java.awt.Rectangle(0,0,4,5); Then access the public fields of this JavaObject r using code such as:
var perimeter = 2*r.width + 2*r.height; JavaPackage ObjectA JavaScript representation of a Java package. A package in Java is a collection of related classes. In JavaScript, a JavaPackage can contain classes (represented by the JavaClass object) and it can also contain other JavaPackage objects. The property naming scheme for the JavaPackage hierarchy mirrors the naming scheme for Java packages. However, the JavaPackage object named java does not actually represent a package in Java, but is simply a convenient placeholder for other JavaPackages that do represent java.lang, javat, java.io, and other important Java classes. Think of the JavaPackage object as representing a Java package representing a directory in the Java class hierarchy. The java JavaPackage object is actually a property of every Window object, which makes it a "global" variable in client-side JavaScript. Since every JavaScript expression is evaluated in the context of one window or another, you can always just use java and know that you will be referring to the JavaPackage object you want. There are other global JavaPackage objects as well (sun, netscape). The Packages property is a JavaPackage object that contains references to each of these java, sun, and netscape JavaPackages. Available in Netscape 3.0. See also JavaArray, JavaClass, JavaObject, Packages.
package.package_name refers to another JavaPackage package.class_name refers to a JavaClass object PropertiesThe properties of a JavaPackage object are the names of the JavaPackage objects and JavaClass objects that it contains. These properties will be different for each individual JavaPackage. Note that it is not possible to use the JavaScript for/in loop to iterate over the list of property names of a Package object; consult a Java reference manual, or examine the Java class hierarchy, to determine the packages and classes contained within any given package. UsageYou can use JavaPackage objects to refer to any Java class. The java.lang.System class, for example, is:
java.lang.System Or:
Packages.java.lang.System Similarly, the netscape.javascript.JSObject class is:
Packagestscape.javascript.JSObject Link ObjectRepresents a hypertext link or a clickable area of a client-side image map in an HTML document. A subclass of the Location object; however, Link differs in that it does not load a new URL automatically (i.e., it changes the URL that the link refers to, but the URL is not displayed until the user selects it). Note that in JavaScript, a hypertext link is a Link object, and a named link destination is an Anchor object. Enhanced in Netscape 3.0. See also Anchor, Location.
document.links[] document.links.length Properties
Event handlersThe values of the following attributes may be any number of JavaScript statements separated by semicolons.
HTML syntaxA Link object is created with standard <a> and </a> tags, with the addition of the onClick, onMouseOver, and onMouseOut event-handler attributes. The HREF attribute is required for all Link objects. If the name attribute is also specified, then an Anchor object is also created:
<A HREF="url" [ name="anchor_tag" ] creates an Anchor object [ target="window_name" ] where the HREF should be displayed [ onClick="handler" ] invoked when link is clicked [ onMouseOver="handler" ] invoked when mouse is over link [ onMouseOut="handler" ] invoked when mouse leaves link > link text or image the visible part of the link </A> In Netscape 3.0 and later, a Link object is also created by each <area> tag within a client-side image map; standard HTML with the addition of event-handler tags:
<MAP name="map_name"> <area SHAPE="area_shape" COORDS=coordinates HREF="url" [ target="window_name" ] where the HREF should be displayed [ onClick="handler" ] invoked when area is clicked [ onMouseOut="handler" ] invoked when mouse leaves area > . . . </MAP>
Location ObjectRepresents a URL. Each of the properties of the Location object is a read/write string that contains one or more portions of the URL described by the object. The location property of Window object is a Location object that specifies the URL of the document. Changing properties of a Location object of a Window causes the browser to read in the changed URL. To load a new URL, you usually set the location property to a string; or you can set any of the properties of the Location object instead. The href property is commonly used. If you just set the hash property of the window.location object, the browser will jump to the newly specified anchor. When you set the location or location.href properties to a URL that you have already visited, the browser will either load that URL from the cache, or will check with the server to see if the document has changed and reload it if necessary. In Netscape 2.0, it will always check with the Web server. In 3.0, the action it takes depends on the Verify Document setting in Netscape's Network Preferences. See also Document, Link, Window.
location window.location document.links[] PropertiesThe fields of a Location object refer to the various portions of a URL, which has the following general format:
protocol://hostname:port/pathname?search#hash
Methods
Math ObjectRead-only reference to a placeholder object that contains mathematical functions and constants. Math is itself an object, not a class of objects, so its constants and methods are invoked directly through it. Math is actually a global property of the Window object, and as such, is usually referred to as Math, rather than as window.Math. random() function added in 3.0. See also Number.
Math.constant Math.function() Invoke functions and constants as follows:
y = Math.sin(x); area = radius * radius * Math.PI;
MimeType ObjectRepresents a MIME datatype supported by the browser (or through a "helper application" or a plug-in for embedded data). Available in Netscape 3.0. See also Netscape, Plugin.
navigator.mimeTypes[i] navigator.mimeTypes["name"] navigator.mimeTypes.length Properties
UsageThe navigator.mimeTypes[] array may be indexed numerically, or with the name of the desired MIME type (which is the value of the name property). To check which MIME types are supported by the browser, you can loop through each element in the array numerically. Or, if you just want to check whether a specific type is supported, you can write code like the following:
var show_movie = (navigator.mimeTypes["video/mpeg"] != null); Navigator ObjectContains properties that describe the Web browser in use; these can be used to perform platform-specific customization. There is only a single instance of the Navigator object, which you can reference through the navigator property of any Window object. Enhanced in Netscape 3.0. See also MimeType, Plugin.
navigator Properties
Methods
Number ObjectNumbers are a basic, primitive data type in JavaScript. In Netscape 3.0, JavaScript also supports the Number object, an object type that represents a primitive numeric value. JavaScript automatically converts between the primitive and object forms as necessary. In JavaScript 3.0, you can explicitly create a Number object with the Number() constructor, although there is rarely any need to do so. Available in Netscape 3.0. See also Math, Number().
Number.constant The Number() constructor:
new Number(value is actually more commonly used as a placeholder for five useful numeric constants. Note that these values are properties of the Number() constructor function itself, not of individual number objects. For example, you use the MAX_value property as follows:
biggest = Number.MAX_value not like this:
n = new Number(2); biggest = n.MAX_value Constants
Methods
Object ObjectA built-in datatype of the JavaScript language; serves as the "superclass" for all other JavaScript objects, and therefore methods of the Object object are also methods of all other object types. The behavior of the Object object is also shared by all other object types. When an Object object is newly created, it has no properties defined; you can add a property definition to an object simply by assigning a value to the property. Objects can also be used as associative arrays. A number of the Object methods can be defined for any object, and will be invoked by the JavaScript system at appropriate times, to perform some sort of operation on the object (e.g., toString). JavaScript allows object syntax to be used to refer to properties and methods of primitive datatypes, such as JavaScript strings. JavaScript creates a temporary object "wrapper" for the primitive value so that the method can be invoked or the property accessed. Enhanced in Netscape 3.0. See also Array, Boolean, Function, Number, String, Window.
new Object(); new Object(value); // Netscape 3.0 and later In Netscape 3.0 and later, the optional value argument may specify a value of any primitive JavaScript type: a number, a boolean, a string, or a function. If no value argument is passed, this constructor returns a newly created object, which has no properties defined. If a value argument is specified, then the constructor creates and returns a Number, Boolean, String, or Function object wrapper around the primitive value. Methods
ExampleDefining the toString(), method, and also the less frequently used assign() and valueOf() methods of an object, is most efficiently done in a constructor method for your object type, or with the prototype object of your object.
// define a constructor for the Complex object type function Complex(x,y) { this.x = x; this.y = y; } // give it a toString() method Complex.prototype.toString = new Function("return '{' + this.x + ',' + this.y + '}';"); // Create an object of this new Complex type c = new Complex(2, 2); // Convert the object to a string, implicitly invoking the // toString() method, and display the string. alert("c = " + c); Option ObjectDescribes a single option displayed within a Select object. Note that although the text displayed by this option is specified outside of the <option> tag, that text must be plain, unformatted text, without any HTML tags. This is so that the text can be properly displayed in list boxes and drop-down menus that do not support HTML formatting. Enhanced in Netscape 3.0. See also Select.
select.options[i] You can dynamically create new Option objects for display in a Select object with the Option() constructor. Once a new Option object is created, it can be appended to the list of options in a Select object by assigning it to options[options.length]. See Select.options[]. Properties
HTML syntaxAn Option object is created by an <option> tag within a <select> which is itself within a <form>. Multiple <option> tags typically appear within the <select>.
<form ...> <select ...> <option [ value="value" ] the value returned when the form is submitted [ selected ] > specifies whether this option is initially selected plain_text_label the text to display for this option ... </select> ... </form> Packages ObjectAn object that contains references to other JavaPackage objects and to JavaClass objects. Each JavaPackage object represents a node in the tree of package names. The Packages property refers to a JavaPackage object which is the root of this package name hierarchy. The Packages object is a "global" variable in JavaScript; a read-only reference to a JavaPackage object, it is defined as a property of all Window objects. Thus, you can always refer to it simply as Packages, rather than explicitly accessing it through a particular Window object. Note that the Window object also contains "global" properties named java, netscape, and sun, all of which are synonyms for the properties of the Packages object. So instead of writing Packages.java.lang.Math, for example, you can just write java.lang.Math. Available in Netscape 3.0. See also JavaClass, JavaObject, JavaPackage. Properties
parseFloat( ) FunctionParses and returns the first number that occurs in s (i.e., converts a string to a number). Parsing stops, and the value is returned, when parseFloat() encounters a character in s that is not a valid part of the number (i.e., a sign, a digit, decimal point, exponent, etc.). If s does not begin with a number that parseInt() can parse, then the function returns NaN, a reserved value that represents "not-a-number." parseFloat() is a built-in JavaScript function; not a method of any object. Buggy in Netscape 2.0. See also isNaN(), parseInt().
parseFloat(s) // s is the string to be parsed and coverted to a number parseInt( ) FunctionParses and returns the first number that occurs in the string s (i.e., it converts a string to an integer). Parsing stops, and the value is returned, when parseInt() encounters a character in s that is not a valid numeral for the specified radix. If s does not begin with a number that parseInt() can parse, then the function returns NaN, a reserved value that represents "not-a-number." Specifying a radix of 10 makes the parseInt() parse a decimal number. The value 8 specifies that an octal number (using digits 0 through 7) is to be parsed. The value 16 specifies a hexadecimal value, using digits 0 through 9 and letters A through F. radix can be any value between 2 and 36. If radix is 0, or if it is not specified, parseInt() tries to determine the radix of the number from s. If s begins with 0x, then parseInt() parses the remainder of s as a hexadecimal number. If s begins with a 0, then parseInt() parses the number in octal. Otherwise, if s begins with a digit 1 through 9, then parseInt() parses it as a decimal number. parseInt is a built-in JavaScript function, not a method of any object. Buggy in Netscape 2.0. See also isNaN(), parseFloat().
parseInt(s) parseInt(s, radix) //s is the string to be parsed //radix is the integer base of the number to be parsed Password ObjectA text input field intended for input of sensitive data, such as passwords. As the user types characters, only asterisks appear. The value property is a read/write string that initially contains the value specified by the value attribute; it specifies the data to be transmitted if the user does not type anything. For security reasons, this default value is the only thing that JavaScript has access to. The user's input is transmitted to the server when the form is submitted, but that input does not appear in this property, and setting this property has no effect on the value transmitted. Enhanced in Netscape 3.0. See also Element, Form, Text.
form.name form.elements[i] form.elements['name'] Properties
Methods
HTML syntaxA Password object is created with a standard HTML <input> tag:
<form> ... <input type="password" specifies that this is a Password object [ name="name" ] a name that can later be used to refer to this object specifies the name property [ value="default" ] the default value transmitted when the form is submitted [ size=integer ] how many characters wide the object is > ... </form> Plugin ObjectRepresents a plug-in application that has been installed in the browser. Available in Netscape 3.0. See also Netscape, MimeType.
navigator.plugins[i] navigator.plugins['name'] navigator.plugins.length Properties
UsageThe navigator.plugins[] array may be indexed numerically when you want to loop through the complete list of installed plug-ins, looking for one that meets your needs. The navigator.plugins[] array can also be indexed by plug-in name, however. That is, if you want to check whether a specific plug-in is installed in the user's browser, you might use code like this:
document.write( navigator.plugins("Shockwave") ? "<EMBED src="movie.dir' height=100 WIDTH=100>" : "You don't have the Shockwave plugin!" ); The name used as an array index with this technique is the same name that appears as the value of the name property. Radio ObjectRepresents a single graphical radio button in an HTML form. Note that the text that appears next to a Radio button is not part of the Radio object itself, and must be specified externally to the <input> tag. The Radio button object is always used in groups of mutually-exclusive options that have the same name. To references on Radio objects within a group, use the syntax below. Note that only one Radio object in a group may contain the checked attribute, which sets the initial values of the checked and defaultChecked properties (true for that object and false for all other Radio buttons in the group). If none of the objects have the checked attribute, then the first one in the group will be checked (and defaultChecked) by default. In Netscape 2.0, there is a bug in how Radio objects in a group are assigned to an array. The workaround is to always assign an event-hander, if only a dummy one, to all of your Radio objects that will be manipulated with JavaScript. Enhanced in Netscape 3.0. See also Checkbox, Element, Form.
form.radio_name[j] form.radio_name.length form.elements[i][j] form.elements[i].length form.elements['radio_name'][j] form.elements['radio_name'].length Properties
Event handlers
HTML syntaxA Radio object is created with a standard HTML <input> tag, with the addition of the new onClick attribute. Radio objects are created in groups by specifying multiple <input> tags that have the same name attribute (mandatory if the radio is part of a form that will submit data to a CGI script). Specifying a name attribute sets the name property, and also allows you to refer to the button by name (instead of as a member of the form elements array).
<form> ... <input type="radio" specifies that this is a radio button [ name="name" ] a name that can later be used to refer to this button or to the group of buttons with this name; specifies the name property [ value="value" ] the value returned when this button is selected; specifies the value property [ checked ] specifies that the button is initially checked; specifies the defaultChecked property [ onClick="handler" ] JavaScript statements to be executed when the button is clicked > label the HTML text that should appear next to the button ... </form> Reset ObjectThe Reset object has the same properties and methods as the Button object, but is used only to reset a form's values (to their defaults). For most elements this means to the value specified by the HTML value attribute. If no initial value was specified, then a click on the Reset button will "clear" any user input from those objects. If no value attribute is specified for a Reset object, it will be labeled "Reset." Enhanced in Netscape 3.0. See also Button, Element, Form.
form.name form.elements[i] form.elements['name'] Properties
Event handlers
HTML syntaxA Reset object is created with a standard HTML <input> tag, with the addition of the onClick attribute:
<form> ... <input type="reset" specifies that this is a Reset 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> Select ObjectRepresents a graphical list of choices from which the user may select. If the multiple attribute is present in the HTML definition of the object, then the user may select any number of options from the list. If that attribute is not present, then the user may select only one option, and options have a "radio button" (i.e., mutually exclusive toggle) behavior. If the size attribute has a value greater than 1, or if the multiple attribute is present, Select objects are displayed in a list box that is size lines high in the browser window. If size is smaller than the number of options, then the list box will include a scrollbar so that all the options are accessible. On the other hand, if size is specified as 1, and multiple is not specified, then the currently selected option is displayed on a single line and the list of other options is made available through a drop-down menu. Enhanced in Netscape 3.0. See also Element, Form, Option.
form.name form.elements[i] form.elements['name'] Properties
Event handlers
HTML syntaxA Select object is created with a standard HTML <select> tag, with the addition of the new onChange, onBlur, and onFocus event-handler attributes. Options to appear within the Select object are created with the <option> tag:
<form> ... <SELECT name="name name identifying this object; specifies name property [ SIZE=integer ] number of visible options in select object [ MULTIPLE ] multiple options may be selected, if present [ onChange="handler" ] invoked when the selection changes [ onBlur="handler" ] invoked when object loses focus [ onFocus="handler" ] invoked when object gains focus > <option value="value1" [selected]> option_label1 <option value="value2" [selected]> option_label2 . . other options here . </select> ... </form> String ObjectExists to provide methods for operating on string values (a basic JavaScript data type). The String class defines a number of methods, most of which simply make a copy of the string with HTML tags added before and after. The string datatype and the String object are not the same, but in Netscape 2.0 are indistinguishable. In Netscape 3.0, you can use the typeof operator to distinguish them (a string has type "string" and a String object has type "object"); however, you can use them interchangeably because JavaScript converts between these two types whenever necessary. When you invoke a String object method on a string value, JavaScript converts that value to a temporary String object, allowing the method to be invoked. In Netscape 3.0, you can use the String object constructor method to create String objects that are not temporary, and that can actually be used by your programs:
new String(value) // Netscape 3.0 only Enhanced in Netscape 3.0. Properties
Methods
UsageA number of the String methods are used for creating HTML:
link_text = "My Home Page".bold(); document.write(link_text.link("http://www.djf.com/~david")); The code above code embeds the following string into the HTML document that is currently being parsed:
<A HREF="http://www.djf.com/~david"><B>My Home Page</B></A> The following code extracts the 3rd through 5th characters of a string and converts them to upper-case letters:
s.substring(2,5).toUpperCase();
Submit ObjectWhen a Submit button is clicked on, it submits the data in the form that contains the button to the server specified by the form's action attribute, and loads the resulting HTML page sent back by that server. The Submit object has the same properties and methods as the Button object. If no value attribute is specified for a Submit object, it will be labelled "Submit Query." Form data may also be submitted by invoking the Form.submit() method. The Submit.onclick() event handler can define additional JavaScript statements to be executed when a Submit button is clicked; to cancel a form submission, use Form.onsubmit(). Enhanced in Netscape 3.0. See also Button, Element, Form.
form.name form.elements[i] form.elements['name'] Properties
Event handlers
HTML syntaxA Reset object is created with a standard HTML <input> tag, with the addition of the onClick attribute:
<form> ... <input type="submit" specifies that this is a Submit 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 button is clicked > ... </form> taint( ) FunctionTaints a value or window (when the data tainting security model is in effect). taint() does not taint the value it is passed; instead, it returns a tainted copy of that value, or a tainted reference to that value for object types. (Note that taint is associated with primitive values and with references to objects, not with the objects themselves.) Sometimes taint is carried not by data values, but by the control flow of a program. In this case, you may want to add taint to the entire window in which JavaScript code runs by calling taint() with no arguments. Available in Netscape 3.0. See also untaint().
taint() taint(value) Text ObjectRepresents a text input field in a form. The size attribute specifies the width, in characters, of the input field as it appears on the screen, and the maxlength attribute specifies the maximum number of characters the user will be allowed to enter. You can read the value property to obtain the user's input, or you can set it to display arbitrary (unformatted) text in the input field. Use the Password object instead of the Text object when the value you are asking the user to enter is sensitive information. Use a Textarea object to allow the user to enter multiple lines of text. When a form contains only one Text or Password object, then the form will automatically be submitted if the user strikes the Return key in that Text or Password object. Enhanced in Netscape 3.0. See also Element, Form, Password, Textarea.
form.name form.elements[i] form.elements['name'] Properties
Methods
Event handlersThe value of the following event handlers may be any number of JavaScript statements, separated by semicolons, which are executed when the handler is invoked.
HTML syntaxA Text object is created with a standard HTML <input> tag, with the addition of optional attributes for event handlers:
<form> ... <input type="text" specifies that this is a Text object [ name="name" ] a name that can later be used to refer to this object; specifies the name property [ value="default" ] the default value transmitted when form is submitted; specifies the defaultValue 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>
Textarea ObjectRepresents a (mutli-line) text input field in a form. The name attribute specifies a name for the object. This is mandatory if the form is to be submitted, and also provides a convenient way to refer to the Textarea object from JavaScript code. Read the value property to obtain the user's input, or set it to display arbitrary (unformatted) text in the Textarea. The initial value of the value property (and the permanent value of the defaultValue property) is the text that appears between the <textarea> and </textarea> tags. If you need only a single line of input text, use the Text object. If the text to be input is sensitive information, such as a password, use the Password object. Enhanced in Netscape 3.0. See also Element, Form, Password, Text.
form.name form.elements[i] form.elements['name'] Properties
Methods
Event handlersThe value of the following event handlers may be any number of JavaScript statements, separated by semicolons, which are executed when the handler is invoked.
HTML syntaxA Textarea object is created with standard HTML <textarea> and </textarea> tags, with the addition of optional attributes for event-handlers. Note that the wrap attribute, which specifies how long lines should be handled, has three legal values: off specifies that they should be left as is; virtual specifies that they should be displayed with line breaks but transmitted without; physical specifies that they should be displayed and transmitted with line breaks inserted.
<form> ... <textarea [ name="name" ] a name that can later be used to refer to this object [ rows=integer ] how many lines tall the object is [ cols=integer ] how many characters wide the object is [ wrap=off|virtual|physical ] how word wrapping is handled [ onBlur="handler" ] the onblur() event handler [ onChange="handler" ] the onchange() event handler [ onFocus="handler" ] the onfocus() event handler > plain_text The initial text; specifies defaultValue </textarea> ... </form>
unescape( ) FunctionThe unescape() function is a built-in part of JavaScript; it is not a method of any object. unescape() decodes a string encoded with escape() and returns the decoded copy.
unescape(s) // s is the string to be decoded or "unescaped" Available in Netscape 2.0. See also escape(), String. Usageunescape decodes s by finding and replacing character sequences of the form %xx, where xx is two hexadecimal digits. Each such sequence is replaced by the single character represented by the hexadecimal digits in the Latin-1 encoding. Thus, unescape() decodes the string:
Hello%20World%21 to:
Hello World! untaint( ) FunctionUntaints a value or window (when the data tainting security model is in effect). untaint() does not remove the taint of the value it is passed; instead, it returns an untainted copy of that value, or an untainted reference to that value for object types. (Note that taint is associated with primitive values and with references to objects, not with the objects themselves.) JavaScript automatically associates taint with data values that are potentially private, and that should not be "stolen" by scripts. If you need to allow these values to be exported by scripts, you must use untaint() to make untainted copies. Sometimes taint is carried not by data values, but by the control flow of a program. In this case, you may need to remove taint from an entire window in which JavaScript code runs. You can do this by calling untaint() with no arguments. Note, however, that you can only do this if the window carries only the taint of the script that calls untaint(). If the window has also been tainted by other scripts, then it cannot be untainted. Available in Netscape 3.0. See also taint().
untaint() untaint(value) Window ObjectRepresents a Web browser window or frame. Since JavaScript code is evaluated in the context of the Window object in which it is running, the Window object must contain references (or references to references) to all the other JavaScript objects of interest (i.e., it is the root of a JavaScript "object hierarchy"). Many of the properties of the Window object are references to other important JavaScript objects. Most of these properties refer to an object particular to the window. The location property of a Window, for example, refers to the Location object of the window. Still other Window properties (e.g., navigator) refer to "global" objects, while a couple refer only to the window itself. In client-side JavaScript, no special syntax is required to refer to the current window, and you can use the properties of that window object as if they were variables (e.g., you can write document rather than window.document). Similarly, you can use the methods of the current window object as if they were functions (e.g., alert() instead of window.alert()).
self the current window window the current window To refer to a frame within a window, use:
frames[i] // or self.frames[i] window.frames[i] To refer to the parent window (or frame) of a frame, use:
parent // or self.parent, window.parent window.parent // parent of specified frame To refer to the top-level browser window from any frame contained within it, use:
top // or self.top, window.top New top-level browser windows are created with the Window.open() method. When you call this method, save the return value of the open() call in a variable, and use that variable to reference the new window. Enhanced in Netscape 3.0. See also Frame. Properties
Methods
Event handlersThe value of the following event handlers may be any number of JavaScript statements, separated by semicolons, which are executed when the handler is invoked.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|