A built-in object that has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi.
To use a Math object:
1. Math.propertyName 2. Math.methodName(parameters)propertyName is one of the properties listed below.
The Math object is a built-in JavaScript object.
You reference the constant PI as Math.PI. Constants are defined with the full precision of real numbers in JavaScript.
Similarly, you reference Math functions as methods. For example, the sine function is Math.sin(argument), where argument is the argument.
It is often convenient to use the with statement when a section of code uses several Math constants and methods, so you don't have to type "Math" repeatedly. For example,
with (Math) { a = PI * r*r y = r*sin(theta) x = r*cos(theta) }
See the examples for the individual properties and methods.
Returns the greater of two numbers.
Math.max(number1, number2)number1 and number2 are any numeric arguments or the properties of existing objects.
//Displays the value 20 document.write("The maximum value is " + Math.max(10,20)) //Displays the value -10 document.write("<P>The maximum value is " + Math.max(-10,-20))
A string specifying how form field input information is sent to the server.
formName.method
formName is either the name of a form or an element in the forms array.
The method property is a reflection of the METHOD attribute of the <FORM> tag. The method property should evaluate to either "get" or "post".
You can set the method property at any time.
Certain values of the method property may require specific values for other form properties. See RFC 1867 for more information.
The following function returns the value of the musicForm method property:
function getMethod() { return document.musicForm.method }
Returns the lesser of two numbers.
Math.min(number1, number2)number1 and number2 are any numeric arguments or the properties of existing objects.
//Displays the value 10 document.write("The minimum value is " + Math.min(10,20)) //Displays the value -20 document.write("<P>The minimum value is " + Math.min(-10,-20))
A string specifying the name of an object.
1. objectName.name 2. frameReference.name 3. frameReference.frames.name 4. radioName[index].name 5. selectName.options.name 6. windowReference.name 7. windowReference.frames.name
objectName is either the value of the NAME attribute of any of the objects listed below or an element in the elements array.
frameReference is a valid way of referring to a frame, as described in the frame object.
radioName is the value of the NAME attribute of a radio object.
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
windowReference is a valid way of referring to a window, as described in the window object.
The value of the name property differs between the window object and other objects.
The name property for the window object is represented by form 6 and form 7 of the syntax. The name property represents the value of the windowName argument described in the window object syntax. Both forms of the syntax represent the same value.
name is a read-only property.
The name property for all objects except window is represented by forms 1 through 5 of the syntax. For all objects except window, the name property initially reflects the value of the NAME attribute. Changing the name property overrides this setting.
You can set the name property at any time.
The name property is the same for every radio button in a single radio object. Individual radio buttons are referenced by their position in the radio array.
Do not confuse the name property with the label displayed on a button, reset, or submit object. The value property specifies the label for these objects. The name property is not displayed onscreen; it is used to reference the objects programatically.
For a select object, the values specified by form 1 and form 5 of the syntax are the same. For a frame object, the values specified by forms 1, 2, and 3 of the syntax are the same.
If multiple objects on the same form have the same NAME attribute, an array of the given name is created automatically. Each element in the array represents an individual form object. Elements are indexed in source order starting at 0. For example, if two text elements and a textarea element on the same form have their NAME attribute set to "myField", an array with the elements myField[0], myField[1], and myField[2] is created.
In the following example, the valueGetter() function uses a for loop to iterate over the array of elements on the valueTest form. The msgWindow window displays the names of all the elements on the form:
newWindow=window.open("http://www.netscape.com") function valueGetter() { var msgWindow=window.open("") for (var i = 0; i < newWindow.document.valueTest.elements.length; i++) { msgWindow.document.write(newWindow.document.valueTest.elements[i].name + "<BR>") } }In the following example, the first statement creates a window called netscapeWin. The second statement displays the value "netscapeHomePage" in the alert dialog box, because "netscapeHomePage" is the value of the windowName argument of netscapeWin.
netscapeWin=window.open("http://www.netscape.com", "netscapeHomePage") alert(netscapeWin.name)
For button, reset, and submit:
Contains information about the version of Navigator in use.
To use a navigator object:
navigator.propertyNamepropertyName is one of the properties listed below.
Use the navigator object to determine which version of the Navigator your users have.
See the examples for the individual properties.
A blur event occurs when a select, text, or textarea field on a form loses focus. The onBlur event handler executes JavaScript code when a blur event occurs.
See the relevant objects for the onBlur syntax.
In the following example, userName is a required text field. When a user attempts to leave the field, the onBlur event handler calls the required() function to confirm that userName has a legal value.
<INPUT TYPE="text" VALUE="" NAME="userName" onBlur="required(this.value)">
A change event occurs when a select, text, or textarea field loses focus and its value has been modified. The onChange event handler executes JavaScript code when a change event occurs.
Use the onChange event handler to validate data after it is modified by a user.
See the relevant objects for the onChange syntax.
In the following example, userName is a text field. When a user attempts to leave the field, the onBlur event handler calls the checkValue() function to confirm that userName has a legal value.
<INPUT TYPE="text" VALUE="" NAME="userName" onBlur="checkValue(this.value)">
A click event occurs when an object on a form is clicked. The onClick event handler executes JavaScript code when a click event occurs.
See the relevant objects for the onClick syntax.
button, checkbox, radio, link, reset, submit
For example, suppose you have created a JavaScript function called compute(). You can execute the compute() function when the user clicks a button by calling the function in the onClick event handler, as follows:
<INPUT TYPE="button" VALUE="Calculate" onClick="compute(this.form)">
In the above example, the keyword this refers to the current object; in this case, the Calculate button. The construct this.form refers to the form containing the button.
For another example, suppose you have created a JavaScript function called pickRandomURL() that lets you select a URL at random. You can use the onClick event handler of a link to specify a value for the HREF attribute of the <A> tag dynamically, as shown in the following example:
In the above example, the onMouseOver event handler specifies a custom message for the Navigator status bar when the user places the mouse pointer over the Go! anchor. As this example shows, you must return true to set the window.status property in the onMouseOver event handler.
A focus event occurs when a field receives input focus by tabbing with the keyboard or clicking with the mouse. Selecting within a field results in a select event, not a focus event. The onFocus event handler executes JavaScript code when a focus event occurs.
See the relevant objects for the onFocus syntax.
The following example uses an onFocus handler in the valueField textarea object to call the valueCheck() function.
<INPUT TYPE="textarea" VALUE="" NAME="valueField" onFocus="valueCheck()">
A load event occurs when Navigator finishes loading a window or all frames within a <FRAMESET>. The onLoad event handler executes JavaScript code when a load event occurs.
Use the onLoad event handler within either the <BODY> or the <FRAMESET> tag, for example, <BODY onLoad="...">.
In a <FRAMESET> and <FRAME> relationship, an onLoad event within a frame (placed in the <BODY> tag) occurs before an onLoad event within the <FRAMESET> (placed in the <FRAMESET> tag).
In the following example, the onLoad event handler displays a greeting message after a web page is loaded.
<BODY onLoad="window.alert("Welcome to the Brave New World home page!")>
onUnload event handler
A mouseOver event occurs once each time the mouse pointer moves over an object from outside that object. The onMouseOver event handler executes JavaScript code when a mouseOver event occurs.
You must return true within the event handler if you want to set the status or defaultStatus properties with the onMouseOver event handler.
See the relevant objects for the onMouseOver syntax.
By default, the HREF value of an anchor displays in the status bar at the bottom of the Navigator when a user places the mouse pointer over the anchor. In the following example, the onMouseOver event handler provides the custom message "Click this if you dare."
See onClick for an example of using onMouseOver when the <A> tag's HREF attribute is set dynamically.
A select event occurs when a user selects some of the text within a text or textarea field. The onSelect event handler executes JavaScript code when a select event occurs.
See the relevant objects for the onSelect syntax.
The following example uses an onSelect handler in the valueField text object to call the selectState() function.
<INPUT TYPE="text" VALUE="" NAME="valueField" onSelect="selectState()">
A submit event occurs when a user submits a form. The onSubmit event handler executes JavaScript code when a submit event occurs.
You can use the onSubmit event handler to prevent a form from being submitted; to do so, put a return statement that returns false in the event handler. Any other returned value lets the form submit. If you omit the return statement, the form is submitted.
See the relevant objects for the onSubmit syntax.
In the following example, the onSubmit event handler calls the formData() function to evaluate the data being submitted. If the data is valid, the form is submitted; otherwise, the form is not submitted.
form.onSubmit="return formData(this)"
See also the examples for the form object.
An unload event occurs when you exit a document. The onUnload event handler executes JavaScript code when an unload event occurs.
Use the onUnload event handler within either the <BODY> or the <FRAMESET> tag, for example, <BODY onUnload="...">.
In a <FRAMESET> and <FRAME> relationship, an onUnload event within a frame (placed in the <BODY> tag) occurs before an onUnload event within the <FRAMESET> (placed in the <FRAMESET> tag).
In the following example, the onUnload event handler calls the cleanUp() function to perform some shut down processing when the user exits a web page:
<BODY onUnload="cleanUp()">
onLoad event handler
Opens a stream to collect the output of write or writeln methods.
document.open(["mimeType"])
mimeType specifies any of the following document types:
text/html text/plain image/gif image/jpeg image/x-bitmap plugInplugIn is any two-part plug-in MIME type that Netscape supports.
The open method opens a stream to collect the output of write or writeln methods. If the mimeType is text or image, the stream is opened to layout; otherwise, the stream is opened to a plug-in. If a document exists in the target window, the open method clears it.
End the stream by using the document.close() method. The close method causes text or images that were sent to layout to display. After using document.close(), issue document.open() again when you want to begin another output stream.
mimeType is an optional argument that specifies the type of document to which you are writing. If you do not specify a mimeType, the open method assumes text/html by default.
Following is a description of mimeType:
function windowWriter1() { var myString = "Hello, world!" msgWindow.document.open() msgWindow.document.write("<P>" + myString) msgWindow.document.close() }In the following example, the probePlugIn() function determines whether a user has the Shockwave plug-in installed:
function probePlugIn(mimeType) { var havePlugIn = false var tiny = window.open("", "teensy", "width=1,height=1") if (tiny != null) { if (tiny.document.open(mimeType) != null) havePlugIn = true tiny.close() } return havePlugIn } var haveShockwavePlugIn = probePlugIn("application/x-director")
Opens a new web browser window.
[windowVar = ][window].open("URL", "windowName", ["windowFeatures"])
windowVar is the name of a new window. Use this variable when referring to a window's properties, methods, and containership.
URL specifies the URL to open in the new window. See the location object for a description of the URL components.
windowName is the window name to use in the TARGET attribute of a <FORM> or <A> tag. windowName can contain only alphanumeric or underscore (_) characters.
windowFeatures is a comma-separated list of any of the following options and values:
toolbar[=yes|no]|[=1|0] location[=yes|no]|[=1|0] directories[=yes|no]|[=1|0] status[=yes|no]|[=1|0] menubar[=yes|no]|[=1|0] scrollbars[=yes|no]|[=1|0] resizable[=yes|no]|[=1|0] width=pixels height=pixels
You may use any subset of these options. Separate options with a comma. Do not put spaces between the options.
pixels is a positive integer specifying the dimension in pixels.
The open method opens a new web browser window on the client, similar to choosing New Web Browser from the File menu of the Navigator. The URL argument specifies the URL contained by the new window. If URL is an empty string, a new, empty window is created.
In event handlers, you must specify window.open() instead of simply using open(). Due to the scoping of static objects in JavaScript, a call to open() without specifying an object name is equivalent to document.open().
windowFeatures is an optional, comma-separated list of options for the new window. The boolean windowFeatures options are set to true if they are specified without values, or as yes or 1. For example, open("", "messageWindow", "toolbar") and open("", "messageWindow", "toolbar=1") both set the toolbar option to true. If windowName does not specify an existing window and you do not specify windowFeatures, all boolean windowFeatures are true by default. If you specify any item in windowFeatures, all other Boolean windowFeatures are false unless you explicitly specify them.
Following is a description of the windowFeatures:
In the following example, the windowOpener function opens a window and uses write methods to display a message:
function windowOpener() { msgWindow=window.open("","displayWindow","menubar=yes") msgWindow.document.write ("<HEAD><TITLE>Message window</TITLE></HEAD>") msgWindow.document.write ("<CENTER><BIG><B>Hello, world!</B></BIG></CENTER>") }
The following is an onClick event handler that opens a new client window displaying the content specified in the file sesame.html. The window opens with the specified option settings; all other options are false because they are not specified.
Notice the use of single quotes (') inside the onClick event handler.
An array corresponding to options in a select object (<OPTION> tags) in source order. See select object.
The parent property is a synonym for a window or frame whose frameset contains the current frame.
1. parent.propertyName 2. parent.methodName 3. parent.frameName 4. parent.frames[index]
propertyName is the defaultStatus, status, length, name, or parent property when the calling parent refers to a window object.
propertyName is the length, name, or parent property when the calling parent refers to a frame object.
methodName is any method associated with the window object.
frameName and frames[index] are ways to refer to frames.
The parent property refers to the <FRAMESET> window of a frame. Child frames within a frameset refer to sibling frames by using "parent" in place of the window name as follows: parent.frameName or parent.frames[index]. For example, if the fourth frame in a set has NAME="homeFrame", sibling frames can refer to that frame using parent.homeFrame or parent.frames[3].
You can use parent.parent to refer to the "grandparent" frame or window when a <FRAMESET> tag is nested within a child frame.
The parent property is read-only. The value of the parent property is
<object nameAttribute>where nameAttribute is the NAME attribute if the parent is a frame, or an internal reference if the parent is a window.
See the examples for the frame object.
Returns the number of milliseconds in a date string since January 1, 1970 00:00:00, local time.
Date.parse(dateString)dateString is a string representing a date or a property of an existing object.
The parse method takes a date string (such as "Dec 25, 1995"), and returns the number of milliseconds since January 1, 1970 00:00:00 (local time). This function is useful for setting date values based on string values, for example in conjunction with the setTime method and the Date object.
Given a string representing a time, parse returns the time value. It accepts the IETF standard date syntax: "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time zone abbreviations, but for general use, use a time zone offset, for example "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.
Because the parse function is a static method of Date, you always use it as Date.parse()
, rather than as a method of a date object you created.
If IPOdate is an existing date object, then
IPOdate.setTime(Date.parse("Aug 9, 1995"))
Parses a string argument and returns a floating point number.
parseFloat(string)
string is a string that represents the value you want to parse.
The parseFloat function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself.
parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters.
If the first character cannot be converted to a number, parseFloat returns one of the following values:
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".
The following examples all return 3.14:
parseFloat("3.14") parseFloat("314e-2") parseFloat("0.0314E+2") var x = "3.14" parseFloat(x)
The following example returns "NaN" or 0:
parseFloat("FF2")
Parses a string argument and returns an integer of the specified radix or base.
parseInt(string [,radix])
string is a string that represents the value you want to parse.
radix is an integer that represents the radix of the return value.
The parseInt function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself.
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. ParseInt truncates numbers to integer values.
If the radix is not specified or is specified as 0, JavaScript assumes the following:
If the first character cannot be converted to a number, parseFloat returns one of the following values:
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".
The following examples all return 15:
parseInt("F", 16) parseInt("17", 8) parseInt("15", 10) parseInt(15.99, 10) parseInt("FXX123", 16) parseInt("1111", 2) parseInt("15*3", 10)
The following examples all return "NaN" or 0:
parseInt("Hello", 8) parseInt("0x7", 10) parseInt("FFF", 10)
Even though the radix is specified differently, the following examples all return 17 because the input string begins with "0x".
parseInt("0x11", 16) parseInt("0x11", 0) parseInt("0x11")
A text field on an HTML form that conceals its value by displaying asterisks (*). When the user enters text into the field, asterisks (*) hide anything entered from view.
To define a password object, use standard HTML syntax:
<INPUT TYPE="password" NAME="passwordName" [VALUE="textValue"] SIZE=integer>NAME="passwordName" specifies the name of the password object. You can access this value using the name property.
To use a password object's properties and methods:
1. passwordName.propertyName 2. passwordName.methodName(parameters) 3. formName.elements[index].propertyName 4. formName.elements[index].methodName(parameters)passwordName is the value of the NAME attribute of a password object.
A password object on a form looks as follows:
A password object is a form element and must be defined within a <FORM> tag.
A string specifying the url-path portion of the URL.
1. links[index].pathname 2. location.pathname
index is an integer representing a link object.
The pathname property specifies a portion of the URL. The pathname supplies the details of how the specified resource can be accessed.
You can set the pathname property at any time, although it is safer to set the href property to change a location. If the pathname that you specify cannot be found in the current location, you will get an error.
See Section 3.1 of RFC 1738 for complete information about the pathname.
See the examples for the href property.
The ratio of the circumference of a circle to its diameter, approximately 3.14159.
Math.PI
Because PI is a constant, it is a read-only property of Math.
The following example displays the value of pi:
document.write("The value of pi is " + Math.PI)
A string specifying the communications port that the server uses for communications.
1. links[index].port 2. location.port
index is an integer representing a link object.
The port property specifies a portion of the URL. The port property is a substring of the host property. The host property is the concatenation of the hostname and port properties, separated by a colon. When the port property is not defined, the host property is the same as the hostname property.
You can set the port property at any time, although it is safer to set the href property to change a location. If the port that you specify cannot be found in the current location, you will get an error. If the port property is not specified, it defaults to 80 on the server.
See Section 3.1 of RFC 1738 for complete information about the port.
See the examples for the href property.
Returns base to the exponent power, that is, baseexponent.
Math.pow(base, exponent)base is any numeric expression or a property of an existing object.
//Displays the value 49 document.write("7 to the power of 2 is " + Math.pow(7,2)) //Displays the value 1024 document.write("<P>2 to the power of 10 is " + Math.pow(2,10))
Displays a Prompt dialog box with a message and an input field.
prompt(message, [inputDefault])message is any string or a property of an existing object; the string is displayed as the message.
Use the prompt method to display a dialog box that receives user input. If you do not specify an initial value for inputDefault, the dialog box displays the value <undefined>.
Although prompt is a method of the window object, you do not need to specify a windowReference when you call it. For example, windowReference.prompt() is unnecessary.
prompt("Enter the number of cookies you want to order:", 12)
A string specifying the beginning of the URL, up to and including the first colon.
1. links[index].protocol 2. location.protocol
index is an integer representing a link object.
The protocol property specifies a portion of the URL. The protocol indicates the access method of the URL. For example, a protocol of "http:" specifies Hypertext Transfer Protocol, and a protocol of "javascript:" specifies JavaScript code.
You can set the protocol property at any time, although it is safer to set the href property to change a location. If the protocol that you specify cannot be found in the current location, you will get an error.
The protocol property represents the scheme name of the URL. See Section 2.1 of RFC 1738 for complete information about the protocol.
See the examples for the href property.