abs method
Returns the absolute value of a number.
语法Math.abs(number)
number is any numeric expression or a property of an existing object.
用法
例子
In the following example, the user enters a number in the first text box and presses the Calculate button to display the absolute value of the number. <FORM>
<P>Enter a number:
<INPUT TYPE="text" NAME="absEntry">
<P>The absolute value is:
<INPUT TYPE="text" NAME="result">
<P>
<INPUT TYPE="button" VALUE="Calculate"
onClick="form.result.value = Math.abs(form.absEntry.value)">
</FORM>
acos method
Returns the arc cosine (in radians) of a number.
语法Math.acos(number)
number is a numeric expression between -1 and 1, or a property of an existing object.
用法
描述
The acos method returns a numeric value between 0 and pi radians. If the value of number is outside the suggested range, the return value is always 0.
例子// Displays the value 0
document.write("The arc cosine of 1 is " + Math.acos(1))
// Displays the value 3.141592653589793
document.write("<P>The arc cosine of -1 is " + Math.acos(-1))
// Displays the value 0
document.write("<P>The arc cosine of 2 is " + Math.acos(2))
See also
asin, atan, cos, sin, tan methods
action property
A string specifying a destination URL for form data that is submitted.
语法formName.action
formName is either the name of a form or an element in the forms array.
Property of
描述
The action property is a reflection of the ACTION attribute of the <FORM> tag. Each section of a URL contains different information. See the location object for a 描述 of the URL components.
You can set the action property at any time.
Certain values of the action property may require specific values for other form properties. See RFC 1867 for more information.
例子
The following example sets the action property of the musicForm form to the value of the variable urlName: document.musicForm.action=urlName
See also
encoding, method, target properties
alert method
Displays an Alert dialog box with a message and an OK button.
语法alert("message")
message is any string or a property of an existing object.
用法
描述
Use the alert method to display a message that does not require a user decision. The message argument specifies a message that the dialog box contains.
Although alert is a 用法 the window object, you do not need to specify a windowReference when you call it. For example, windowReference.alert() is unnecessary.
例子
In the following example, the testValue() function checks the name entered by a user in the text object of a form to make sure that it is no more than eight characters in length. This example uses the alert method to prompt the user to enter a valid value. function testValue(textElement) {
if (textElement.length > 8) {
alert("Please enter a name that is 8 characters or less")
}
}
You can call the testValue() function in the onBlur event handler of a form's text object, as shown in the following example: Name: <INPUT TYPE="text" NAME="userName"
onBlur="testValue(userName.value)">
See also
alinkColor property
A string specifying the color of an active link (after mouse-button down, but before mouse-button up).
语法document.alinkColor
Property of
描述
The alinkColor property is expressed as a hexadecimal RGB triplet or as one of the string literals listed in Color Values. This property is the JavaScript reflection of the ALINK attribute of the <BODY> tag. You cannot set this property after the htm source has been through layout.
If you express the color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072".
例子
The following example sets the color of active links to aqua using a string literal: document.alinkColor="aqua"
The following example sets the color of active links to aqua using a hexadecimal triplet: document.alinkColor="00FFFF"
See also
bgColor, fgColor, linkColor, and vlinkColor properties
anchor method
Creates an htm anchor that is used as a hypertext target.
语法text.anchor(nameAttribute)
text is any string or a property of an existing object.
nameAttribute is any string or a property of an existing object.
用法
描述
Use the anchor method with the write or writeln methods to programatically create and display an anchor in a document. Create the anchor with the anchor method, then call write or writeln to display the anchor in a document.
In the 语法, the text string represents the literal text that you want the user to see. The nameAttribute string represents the NAME attribute of the <A> tag.
Anchors created with the anchor method become elements in the anchors array. See the anchor object for information about the anchors array.
例子
The following example opens the msgWindow window and creates an anchor for the Table of Contents: var myString="Table of Contents"
msgWindow=window.open("","displayWindow")
msgWindow.document.writeln(myString.anchor("contents_anchor"))
msgWindow.document.close()
The previous example produces the same output as the following htm: Table of Contents
See also
link method
anchor object (anchors array)
A piece of text that can be the target of a hypertext link.
语法
To define an anchor, use standard htm 语法: <A [HREF=locationOrURL]
NAME="anchorName"
[TARGET="windowName"]>
anchorText
</A>
HREF=locationOrURL identifies a destination anchor or URL. If this attribute is present, the anchor object is also a link object. See link for details.
NAME="anchorName" specifies a tag that becomes an available hypertext target within the current document.
TARGET="windowName" specifies the window that the link is loaded into. This attribute is meaningful only if HREF=locationOrURL is present. See link for details.
anchorText specifies the text to display at the anchor.
You can also define an anchor using the anchor method.
Property of
描述
If an anchor object is also a link object, the object has entries in both the anchors and links arrays.
The anchors array
You can reference the anchor objects in your code by using the anchors array. This array contains an entry for each <A> tag containing a NAME attribute in a document in source order. For example, if a document contains three named anchors, these anchors are reflected as document.anchors[0], document.anchors[1], and document.anchors[2].
To use the anchors array: 1. document.anchors[index]
2. document.anchors.length
index is an integer representing an anchor in a document.
To obtain the number of anchors in a document, use the length property: document.anchors.length.
Even though the anchors array represents named anchors, the value of anchors[index] is always null. But if a document names anchors in a systematic way using natural numbers, you can use the anchors array and its length property to validate an anchor name before using it in operations such as setting location.hash. See the example below.
Elements in the anchors array are read-only. For example, the statement document.anchors[0]="anchor1" has no effect.
Properties
The anchors object has no properties. The anchors array has the following properties:
length reflects the number of named anchors in a document
Methods
None.
Event handlers
None.
例子
Example 1: an anchor. The following example defines an anchor for the text "Welcome to JavaScript". Welcome to JavaScript
If the preceding anchor is in a file called intro.htm, a link in another file could define a jump to the anchor as follows: Introduction
Example 2: anchors array. The following example opens two windows. The first window contains a series of buttons that set location.hash in the second window to a specific anchor. The second window defines four anchors named "0", "1", "2", and "3". (The anchor names in the document are therefore 0, 1, 2, ... (document.anchors.length-1)). When a button is pressed in the first window, the onClick event handler verifies that the anchor exists before setting window2.location.hash to the specified anchor name.
LINK1.htm, which defines the first window and its buttons, contains the following code:
Links and Anchors: Window 1
Links and Anchors
Click a button to display that anchor in window #2
LINK2.htm, which contains the anchors, contains the following code:
Links and Anchors: Window 2
Some numbers (Anchor 0)
one
two
three
four
five
six
seven
eight
nine
Some colors (Anchor 1)
red
orange
yellow
green
blue
purple
brown
black
Some music types (Anchor 2)
R&B
Jazz
Soul
Reggae
Rock
Country
Classical
Opera
Some countries (Anchor 3)
Afghanistan
Brazil
Canada
Finland
India
Italy
Japan
Kenya
Mexico
Nigeria
See also
link object
anchor method
anchors property
An array of objects corresponding to named anchors in source order. See anchor object.
appCodeName property
A string specifying the code name of the browser.
语法navigator.appCodeName
Property of
描述
appCodeName is a read-only property.
例子
The following example displays the value of the appCodeName property: document.write("The value of navigator.appCodeName is " +
navigator.appCodeName)
For Navigator 2.0, this displays the following: The value of navigator.appCodeName is Mozilla
See also
appName, appVersion, userAgent properties
appName property
A string specifying the name of the browser.
语法navigator.appName
Property of
描述
appName is a read-only property.
例子
The following example displays the value of the appName property: document.write("The value of navigator.appName is " +
navigator.appName)
For Navigator 2.0, this displays the following: The value of navigator.appName is Netscape
See also
appVersion, appCodeName, userAgent properties
appVersion property
A string specifying version information for the Navigator.
语法navigator.appVersion
Property of
描述
The appVersion property specifies version information in the following format:
releaseNumber (platform; country)
The values contained in this format are the following:
releaseNumber is the version number of the Navigator. For example, "2.0b4" specifies Navigator 2.0, beta 4.
platform is the platform upon which the Navigator is running. For example, "Win16" specifies a 16-bit version of Windows such as Windows 3.11.
country is either "I" for the international release, or "U" for the domestic U.S. release. The domestic release has a stronger encryption feature than the international release.
appVersion is a read-only property.
例子
Example 1. The following example displays version information for the Navigator: document.write("The value of navigator.appVersion is " +
navigator.appVersion)
For Navigator 2.0 on Windows 95, this displays the following: The value of navigator.appVersion is 2.0 (Win95, I)
Example 2. The following example populates a textarea object with newline characters separating each line. Because the newline character varies from platform to platform, the example tests the appVersion property to determine whether the user is running Windows (appVersion contains "Win" for all versions of Windows). If the user is running Windows, the newline character is set to \r\n; otherwise, it's set to \n, which is the newline character for Unix and Macintosh.
See also
appName, appCodeName, userAgent properties
asin method
Returns the arc sine (in radians) of a number.
语法Math.asin(number)
number is a numeric expression between -1 and 1, or a property of an existing object.
用法
描述
The asin method returns a numeric value between -pi/2 and pi/2 radians. If the value of number is outside the suggested range, the return value is always 0.
例子// Displays the value 1.570796326794897 (pi/2)
document.write("The arc sine of 1 is " + Math.asin(1))
// Displays the value -1.570796326794897 (-pi/2)
document.write("<P>The arc sine of -1 is " + Math.asin(-1))
// Displays the value 0 because the argument is out of range
document.write("<P>The arc sine of 2 is " + Math.asin(2))
See also
acos, atan, cos, sin, tan methods
atan method
Returns the arc tangent (in radians) of a number.
语法Math.atan(number)
number is either a numeric expression or a property of an existing object, representing the tangent of an angle.
用法
描述
The atan method returns a numeric value between -pi/2 and pi/2 radians.
例子// Displays the value 0.7853981633974483
document.write("The arc tangent of 1 is " + Math.atan(1))
// Displays the value -0.7853981633974483
document.write("<P>The arc tangent of -1 is " + Math.atan(-1))
// Displays the value 0.4636476090008061
document.write("<P>The arc tangent of .5 is " + Math.atan(.5))
See also