1. Math.propertyName 2. Math.methodName(parameters)
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) }
Methods
The Math object has the following methods:
Event handlers
None. Built-in objects do not have event handlers.
Examples
See the examples for the individual properties and methods.
max
Method. Returns the greater of two numbers.
Syntax
Math.max(number1, number2)
Parameters
number1 and number2 are any numeric arguments or the properties of existing objects.
Method of
Math
Examples
The following function evaluates the variables x and y:
function getMax(x,y) {
return Math.max(x,y)
}
If you pass getMax the values ten and twenty, it returns twenty; if you pass it the values -10 and -20, it returns -10.
See also
min method
method
Property. A string specifying how form field input information is sent to the server.
Syntax
formName.method
Parameters
formName is either the name of a form or an element in the forms array.
Property of
form
Description
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.
Examples
The following function returns the value of the musicForm method property:
function getMethod() {
return document.musicForm.method
}
See also
action, encoding, target properties; form object
min
Method. Returns the lesser of two numbers.
Syntax
Math.min(number1, number2)
Parameters
number1 and number2 are any numeric arguments or the properties of existing objects.
Method of
Math
Examples
The following function evaluates the variables x and y:
function getMin(x,y) {
return Math.min(x,y)
}
If you pass getMin the values ten and twenty, it returns ten; if you pass it the values -10 and -20, it returns -20.
See also
max method
name
Property. A string specifying the name of an object.
Syntax
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
Parameters
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.
Property of
name is a read-only property.
All other objects
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 on-screen; it is used to reference the objects programmatically.
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 zero. 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.
Examples
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)
See also
For button, reset, and submit: value property
navigator
Object. Contains information about the version of Navigator in use.
Syntax
To use a navigator object:
navigator.propertyName
Parameters
propertyName is one of the properties listed below.
Property of
None
Description
Use the navigator object to determine which version of the Navigator your users have.
Properties
The navigator object has the following properties:
Methods
None
Event handlers
None
Examples
See the examples for the individual properties.
See also
link object, anchor object
onBlur
Event handler. 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.
Event handler of
select object, text object, textarea object
Examples
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)">
See also
onChange, onFocus event handlers
onChange
Event handler. 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.
Event handler of
select object, text object, textarea object
Examples
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)">
See also
onBlur, onFocus event handlers
onClick
Event handler. 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.
Event handler of
button object, checkbox object, link object, radio object, reset object, submit object
Examples
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 preceding 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:
<A HREF=""
onClick="this.href=pickRandomURL()"
onMouseOver="window.status='Pick a random URL'; return true">
Go!</A>
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.
onFocus
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.
Event handler of
select object, text object, textarea object
Examples
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()">
See also
onBlur, onChange event handlers
onLoad
Event handler. A load event occurs when Navigator finishes loading a window or all frames within a FRAMESET tag. 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).
Event handler of
window object
Examples
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!")>
See also
onUnload event handler
onMouseOver
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.
Event handler of
link object
Examples
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."
<A HREF="http://home.netscape.com/"
onMouseOver="window.status='Click this if you dare!'; return true">
Click me</A>
See onClick for an example of using onMouseOver when the A tag's HREF attribute is set dynamically.
onSelect
Event handler. 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.
Event handler of
text object, textarea object
Examples
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()">
onSubmit
Event handler. 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.
Event handler of
form
Examples
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.
See also
submit object, submit method
onUnload
Event handler. 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).
Event handler of
window object
Examples
In the following example, the onUnload event handler calls the cleanUp function to perform some shutdown processing when the user exits a Web page:
<BODY onUnload="cleanUp()">
See also
onLoad event handler
open (document object)
Method. Opens a stream to collect the output of write or writeln methods.
Syntax
document.open(["mimeType"])
Parameters
mimeType is an optional argument that specifies the type of document to which you are writing. If you do not specify a mimeType, text/html
is the default.
text/html
specifies a document containing ASCII text with HTML formatting.
text/plain
specifies a document containing plain ASCII text with end-of-line characters to delimit displayed lines.
image/gif
specifies a document with encoded bytes constituting a GIF header and pixel data.
image/jpeg
specifies a document with encoded bytes constituting a JPEG header and pixel data.
image/x-bitmap
specifies a document with encoded bytes constituting a bitmap header and pixel data.
Method of
document
Description
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.
Examples
The following function calls document.open()
to open a stream before issuing a write method:
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")
See also
clear, close (document object), write, writeln methods
open (window object)
Method. Opens a new web browser window.
Syntax
[windowVar = ][window].open("URL", "windowName", ["windowFeatures"])
Parameters
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. The windowFeatures are:
Method of
window object
Description
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.
Examples
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.
<FORM NAME="myform">
<INPUT TYPE="button" NAME="Button1" VALUE="Open Sesame!"
onClick="window.open ('sesame.html', 'newWin',
'scrollbars=yes,status=yes,width=300,height=300')">
</FORM>
Notice the use of single quotes (') inside the onClick event handler.
See also
close (window object) method
options
Property. An array corresponding to options in a select object (OPTION tags) in source order. See the select object.
parent
Property. The parent property is a synonym for a window or frame whose frameset contains the current frame.
Syntax
1. parent.propertyName
2. parent.methodName
3. parent.frameName
4. parent.frames[index]
Parameters
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.
Property of
frame object, window object
Description
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.
Examples
See the examples for the frame object.
parse
Method. Returns the number of milliseconds in a date string since January 1, 1970, 00:00:00, local time.
Syntax
Date.parse(dateString)
Parameters
dateString is a string representing a date or a property of an existing object.
Method of
Date
Description
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.
Examples
If IPOdate is an existing Date object, then
IPOdate.setTime(Date.parse("Aug 9, 1995"))
See also
UTC method
parseFloat
Function. Parses a string argument and returns a floating point number.
Syntax
parseFloat(string)
Parameters
string is a string that represents the value you want to parse.
Description
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:
Examples
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 zero:
parseFloat("FF2")
See also
isNaN, parseInt functions
parseInt
Function. Parses a string argument and returns an integer of the specified radix or base.
Syntax
parseInt(string [,radix])
Parameters
string is a string that represents the value you want to parse.
radix is an integer that represents the radix of the return value.
Description
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 ten indicates to convert to a decimal number, eight octal, sixteen hexadecimal, and so on. For radixes above ten, the letters of the alphabet indicate numerals greater than ninr. For example, for hexadecimal numbers (base sixteen), 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 zero, JavaScript assumes the following:
Examples
The following examples all return fifteen:
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 zero:
parseInt("Hello", 8)
parseInt("0x7", 10)
parseInt("FFF", 10)
Even though the radix is specified differently, the following examples all return seventeen because the input string begins with "0x."
parseInt("0x11", 16)
parseInt("0x11", 0)
parseInt("0x11")
See also
isNaN, parseFloat functions
password
Object. A text field on an HTML form that conceals its value by displaying asterisks (*). When the user enters text into the field, asterisks (*) hide entries from view.
HTML syntax
To define a password object, use standard HTML syntax:
<INPUT
TYPE="password"
NAME="passwordName"
[VALUE="textValue"]
SIZE=integer>
HTML attributes
NAME="passwordName" specifies the name of the password object. You can access this value using the name property.
VALUE="textValue" specifies the initial value of the password object. You can access this value using the defaultValue property.
SIZE=integer specifies the number of characters the password object can accommodate without scrolling.
Syntax
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)
Parameters
passwordName is the value of the NAME attribute of a password object.
formName is either the value of the NAME attribute of a form object or an element in the forms array.
index is an integer representing a password object on a form.
propertyName is one of the properties listed below.
methodName is one of the methods listed below.
Property of
form
Description
A password object on a form looks as follows:
A password object is a form element and must be defined within a FORM tag.
Properties
The password object has the following properties:
Property | Description |
---|---|
defaultValue | Reflects the VALUE attribute |
name | Reflects the NAME attribute |
value | Reflects the current value of the password object's field |
Methods
Event handlers
None
Examples
The following example creates a password object with no default value:
<B>Password:</B> <INPUT TYPE="password" NAME="password" VALUE="" SIZE=25>
See also
form object, text object
pathname
Property. A string specifying the url-path portion of the URL.
Syntax
1. links[index].pathname
2. location.pathname
Parameters
index is an integer representing a link object.
Property of
link object, location object
Description
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 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html
) for complete information about the pathname.
Examples
See the examples for the href property.
See also
hash, host, hostname, href, port, protocol, search properties
PI
Property. The ratio of the circumference of a circle to its diameter, approximately 3.14159.
Syntax
Math.PI
Property of
Math
Description
Because PI is a constant, it is a read-only property of Math.
Examples
The following function returns the value of pi:
function getPi() {
return Math.PI
}
See also
E, LN2, LN10, LOG2E, LOG10E, SQRT1_2, SQRT2 properties
port
Property. A string specifying the communications port that the server uses for communications.
Syntax
1. links[index].port
2. location.port
Parameters
index is an integer representing a link object.
Property of
link object, location object
Description
The port property specifies a portion of the URL. The port property is a substring of the hostname property. The hostname property is the concatenation of the host and port properties, separated by a colon. When the port property is 80 (the default), 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 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html
) for complete information about the port.
Examples
See the examples for the href property.
See also
hash, host, hostname, href, pathname, protocol, search properties
pow
Method. Returns base to the exponent power, that is, baseexponent.
Syntax
Math.pow(base, exponent)
Parameters
base is any numeric expression or a property of an existing object.
exponent is any numeric expression or a property of an existing object.
Method of
Math
Examples
function raisePower(x,y) {
return Math.pow(x,y)
}
If x equals seven and y equals two, raisePower returns forty-nine (seven to the power of two).
See also
exp, log methods
prompt
Method. Displays a Prompt dialog box with a message and an input field.
Syntax
prompt(message, [inputDefault])
Parameters
message is any string or a property of an existing object; the string is displayed as the message.
inputDefault is a string, integer, or property of an existing object that represents the default value of the input field.
Method of
window object
Description
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 "<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.
Examples
prompt("Enter the number of cookies you want to order:", 12)
See also
alert, confirm methods
protocol
Property. A string specifying the beginning of the URL, up to and including the first colon.
Syntax
1. links[index].protocol
2. location.protocol
Parameters
index is an integer representing a link object.
Property of
link object, location object
Description
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 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html
) for complete information about the protocol.
Examples
See the examples for the href property.
See also
hash, host, hostname, href, pathname, port, search properties