## lastIndexOf method

Returns the index within the calling string object of the last occurrence of the specified value. The calling string is searched backwards, starting at fromIndex.

### 语法

stringName.lastIndexOf(searchValue, [fromIndex])

stringName is any string or a property of an existing object.
searchValue is a string or a property of an existing object, representing the value to search for.
fromIndex is the location within the calling string to start the search from. It can be any integer from 0 to stringName.length - 1 or a property of an existing object.

### 描述

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1.

If you do not specify a value for fromIndex, JavaScript assumes stringName.length - 1 (the end of the string) by default. If searchValue is not found, JavaScript returns -1.

### 例子

The following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".
var anyString="Brave new world"

//Displays 8
document.write("<P>The index of the first w from the beginning is " +
anyString.indexOf("w"))
//Displays 10
document.write("<P>The index of the first w from the end is " +
anyString.lastIndexOf("w"))
//Displays 6
document.write("<P>The index of 'new' from the beginning is " +
anyString.indexOf("new"))
//Displays 6
document.write("<P>The index of 'new' from the end is " +
anyString.lastIndexOf("new"))


### 相关

• charAt, indexOf methods

## lastModified property

### 语法

document.lastModified

### 例子

In the following example, the lastModified property is used in a <SCRIPT> tag at the end of an htm file to display the modification date of the page:

document.write("This page updated on " + document.lastModified)


## length property

An integer that specifies a length-related feature of the calling object or array.

### 语法

When used with objects:

1. formName.length
2. frameReference.length
3. history.length
5. selectName.length
6. stringName.length
7. windowReference.length


When used with array properties:

8. anchors.length
9. elements.length
10. forms.length
11. frameReference.frames.length
12. windowReference.frames.length
14. selectName.options.length


formName is either the name of a form or an element in the forms array.
frameReference is either the value of the NAME attribute of a frame or an element in the frames array.
radioName is either the value of the NAME attribute of a radio object or an element in the elements array.
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
stringName is any string or a property of an existing object.
windowReference is a valid way of referring to a window, as described in the window object.

### Property of

• frame, history, radio, select, string, window objects
• anchors, elements, forms, frames, links, options arrays

### 描述

The length property is an integer that specifies one of the following:

• The number of elements on a form (form 1 of the 语法).
• The number of frames within a frame (form 2 of the 语法). A frame that does not load a document containing a <FRAMESET> tag always has a length of 0.
• The number of entries in a history object (form 3 of the 语法).
• The number of radio buttons in a radio object (form 4 of the 语法).
• The number of options in a select object (form 5 of the 语法).
• The length of a string object (form 6 of the 语法).
• The number of frames in a parent window (form 7 of the 语法).
• The number of entries in one of the array properties (all other 语法 forms).

length is always a read-only property.

For a null string, length is zero. For a select object, the values returned by form 5 and form 14 of the 语法 are the same. For a window containing frames, the values returned by form 7 and form 12 of the 语法 are the same. For a form object, the values returned by form 1 and form 9 of the 语法 are the same. For a frame containing frames, the values returned by form 2 and form 11 of the 语法 are the same.

### 例子

In the following example, the getChoice() function uses the length property to iterate over every element in the musicType array. musicType is a select element on the musicForm form.

function getChoice() {
for (var i = 0; i < document.musicForm.musicType.length; i++) {
if (document.musicForm.musicType.options[i].selected == true) {
return document.musicForm.musicType.options[i].text
}
}
}


The following example displays 8 in an alert dialog box:

var x="Netscape"
alert("The string length is " + x.length)


Creates an htm hypertext link that jumps to another URL.

### 语法

linkText.link(hrefAttribute)

linkText is any string or a property of an existing object.
hrefAttribute is any string or a property of an existing object.

### 描述

Use the link method with the write or writeln methods to programatically create and display a hypertext link in a document. Create the link with the link method, then call write or writeln to display the link in a document.

In the 语法, the linkText string represents the literal text that you want the user to see. The hrefAttribute string represents the HREF attribute of the <A> tag, and it should be a valid URL. Each section of a URL contains different information. See the location object for a 描述 of the URL components.

### 例子

The following example displays the word "Netscape" as a hypertext link that returns the user to the Netscape home page:

var hotText="Netscape"
var URL="http://www.netscape.com"

document.open()
document.close()


The previous example produces the same output as the following htm:

### 相关

• anchor method

A piece of text or an image identified as a hypertext link. When the user clicks the link text, the link hypertext reference is loaded into its target window.

### 语法

To define a link, use standard htm 语法 with the addition of the onClick and onMouseOver event handlers:

<A HREF=locationOrURL
[NAME="anchorName"]
[TARGET="windowName"]
[onClick="handlerText"]
[onMouseOver="handlerText"]>
</A>

HREF=locationOrURL identifies a destination anchor or URL. See the location object for a 描述 of the URL components.
NAME="anchorName" specifies a tag that becomes an available hypertext target within the current document. If this attribute is present, the link object is also an anchor object. See anchor for details.
TARGET="windowName" specifies the window that the link is loaded into. windowName can be an existing window; it can be a frame name specified in a <FRAMESET> tag; or it can be one of the literal frame names _top, _parent, _self, or _blank; it cannot be a JavaScript expression (for example, it cannot be parent.frameName or windowName.frameName).

To use a link object's properties:

document.links[index].propertyName

index is an integer representing a link object.
propertyName is one of the properties listed below.

• document

### 描述

Each link object is a location object and has the same properties as a location object.

If a link object is also an anchor object, the object has entries in both the anchors and links arrays.

When a user clicks a link object and navigates to the destination document (specified by HREF=locationOrURL), the destination document's referrer property contains the URL of the source document. Evaluate the referrer property from the destination document.

You can reference the link objects in your code by using the links array. This array contains an entry for each link object (<A HREF=""> tag) in a document in source order. For example, if a document contains three link objects, these links are reflected as document.links[0], document.links[1], and document.links[2].

1. document.links[index]


index is an integer representing a link in a document.

To obtain the number of links in a document, use the length property: document.links.length.

### Properties

The link object has the following properties:

• hash specifies an anchor name in the URL
• host specifies the hostname:port portion of the URL
• hostname specifies the host and domain name, or IP address, of a network host
• href specifies the entire URL
• pathname specifies the url-path portion of the URL
• port specifies the communications port that the server uses for communications
• protocol specifies the beginning of the URL, including the colon
• search specifies a query
• target reflects the TARGET attribute

The links array has the following properties:

• length reflects the number of links in a document

• None.

• onClick
• onMouseOver

### 例子

Example 1. The following example creates a hypertext link to an anchor named javascript_intro.

<A HREF="#javascript_intro">Introduction to JavaScript</A>

Example 2. The following example creates a hypertext link to an anchor named numbers in the file DOC3.htm in the window window2. If window2 does not exist, it is created.

<LI><A HREF="tppmsgs/msgs0.htm#16" tppabs="http://www.nease.net/~jim/doc3.htm#numbers" TARGET="window2">Numbers</A>

Example 3. The following example takes the user back x entries in the history list:

Example 4. The following example creates a hypertext link to a URL. A set of radio buttons lets the user choose between three URLs. The link's onClick event handler sets the URL (the link's href property) based on the selected radio button. The link also has an onMouseOver event handler that changes the window's status property. As the example shows, you must return true to set the window.status property in the onMouseOver event handler.

<SCRIPT> var destHREF="'http://www.netscape.com/" </SCRIPT> <FORM NAME="form1"> <B>Choose a destination from the following list, then click "Click me" below.</B> <BR><INPUT TYPE="radio" NAME="destination" VALUE="netscape" onClick="destHREF='http://www.netscape.com/'"> Netscape home page <BR><INPUT TYPE="radio" NAME="destination" VALUE="sun" onClick="destHREF='http://www.sun.com/'"> Sun home page <BR><INPUT TYPE="radio" NAME="destination" VALUE="rfc1867" onClick="destHREF='http://www.ics.uci.edu/pub/ietf/htm/rfc1867.txt'"> RFC 1867 <P><A HREF="" onMouseOver="window.status='Click this if you dare!'; return true" onClick="this.href=destHREF"> <B>Click me</B></A> </FORM>

newWindow=window.open("http://www.netscape.com") function linkGetter() { msgWindow=window.open("") for (var i = 0; i < newWindow.document.links.length; i++) { msgWindow.document.write(newWindow.document.links[i] + "<BR>") } }

### 相关

• anchor object

A string specifying the color of the document hyperlinks.

### 语法

document.linkColor

### 描述

The linkColor 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 LINK attribute of the <BODY> tag. The default value of this property is set by the user on the Colors tab of the Preferences dialog box, which is displayed by choosing General Preferences from the Options menu. 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 document links to aqua using a string literal:

document.linkColor="aqua"


The following example sets the color of document links to aqua using a hexadecimal triplet:

document.linkColor="00FFFF"


### 相关

An array of objects corresponding to link objects in source order. See link object.

## LN2 property

The natural logarithm of two, approximately 0.693.

### 语法

Math.LN2

### 描述

Because LN2 is a constant, it is a read-only property of Math.

### 例子

The following example displays the natural log of 2:

document.write("The natural log of 2 is " + Math.LN2)

### 相关

• E, LN10, LOG2E, LOG10E, PI, SQRT1_2, SQRT2 properties

## LN10 property

The natural logarithm of ten, approximately 2.302.

### 语法

Math.LN10

### 描述

Because LN10 is a constant, it is a read-only property of Math.

### 例子

The following example displays the natural log of 10:

document.write("The natural log of 10 is " + Math.LN10)

### 相关

• E, LN2, LOG2E, LOG10E, PI, SQRT1_2, SQRT2 properties

## location object

Contains information on the current URL.

### 语法

To use a location object:

[windowReference.]location[.propertyName]

windowReference is a variable windowVar from a window definition (see window object), or one of the synonyms top or parent.
propertyName is one of the properties listed below. Omitting the property name is equivalent to specifying the href property (the complete URL).

• window

### 描述

The location object represents a complete URL. Each property of the location object represents a different portion of the URL.

The following diagram of a URL shows the relationships between the location properties:

protocol//hostname:port pathname search hash

protocol represents the beginning of the URL, up to and including the first colon.
hostname represents the host and domain name, or IP address, of a network host.
port represents the communications port that the server uses for communications.
pathname represents the url-path portion of the URL.
search represents any query information in the URL, beginning with a question mark.
hash represents an anchor name fragment in the URL, beginning with a hash mark (#).

See the properties (listed below) for details about the different parts of the URL, or the href property for 例子.

The location object has two other properties not shown in the diagram above:

href represents a complete URL.
host represents the concatenation hostname:port.

The location object is contained by the window object and is within its scope. If you reference a location object without specifying a window, the location object represents the current location. If you reference a location object and specify a window name, for example, windowReference.location.propertyName, the location object represents the location of the specified window.

Do not confuse the location object with the location property of the document object. You cannot change the value of the location property (document.location), but you can change the value of the location object's properties (window.location.propertyName). document.location is a string-valued property that usually matches what window.location.href is set to when you load the document, but redirection may change it.

### 语法 for common URL types

When you specify a URL, you can use standard URL formats and JavaScript statements. The following list shows the 语法 for specifying some of the most common types of URLs.

URL type Protocol Example
JavaScript code javascript: javascript:history.go(-1)
World Wide Web http: http://www.netscape.com/
File file: file:///javascript/methods.htm
FTP ftp: ftp://ftp.mine.com/home/mine
MailTo mailto: mailto:info@netscape.com
Usenet news: news://news.scruznet.com/comp.lang.javascript
Gopher gopher: gopher.myhost.com

The javascript: protocol evaluates the expression after the colon (:), if there is one, and loads a page containing the string value of the expression, unless it is undefined. If the expression evaluates to undefined, no new page loads.

The about: protocol provides information on Navigator and has the following 语法:

about:[cache|plugins]


### Properties

• hash specifies an anchor name in the URL
• host specifies the hostname:port portion of the URL
• hostname specifies the host and domain name, or IP address, of a network host
• href specifies the entire URL
• pathname specifies the url-path portion of the URL
• port specifies the communications port that the server uses for communications
• protocol specifies the beginning of the URL, including the colon
• search specifies a query

• None.

• None.

### 例子

Example 1. The following two statements are equivalent and set the URL of the current window to the Netscape home page:

window.location.href="http://www.netscape.com/" window.location="http://www.netscape.com/"

Example 2. The following statement sets the URL of a frame named frame2 to the Sun home page:

parent.frame2.location.href="http://www.sun.com/"

相关 the example for the anchor object.

### 相关

• history object
• location property

## location property

A string specifying the complete URL of the document.

### 语法

document.location

### 描述

Do not confuse the location property of the document object with the location object. You cannot change the value of the location property (document.location), but you can change the value of the location object's properties (window.location.propertyName). document.location is a string-valued property that usually matches what window.location.href is set to when you load the document, but redirection may change it.

location is a read-only property of document.

### 例子

The following example displays the URL of the current document:

document.write("The current URL is " + document.location)


### 相关

• location object

## log method

Returns the natural logarithm (base e) of a number.

### 语法

Math.log(number)
number is any positive numeric expression or a property of an existing object.

### 描述

If the value of number is outside the suggested range, the return value is always -1.797693134862316e+308.

### 例子

//Displays the value 2.302585092994046
document.write("The natural log of 10 is " + Math.log(10))

//Displays the value 0
document.write("<P>The natural log of 1 is " + Math.log(1))

//Displays the value -1.797693134862316e+308
//because the argument is out of range
document.write("<P>The natural log of 0 is " + Math.log(0))


### 相关

• exp, pow methods

## LOG2E property

The base 2 logarithm of e (approximately 1.442).

### 语法

Math.LOG2E

### 描述

Because LOG2E is a constant, it is a read-only property of Math.

### 例子

The following example displays the base 2 logarithm of E:

document.write("The base 2 logarithm of E is " + Math.LOG2E)

### 相关

• E, LN2, LN10, LOG10E, PI, SQRT1_2, SQRT2 properties

## LOG10E property

The base 10 logarithm of e (approximately 0.434).

### 语法

Math.LOG10E

### 描述

Because LOG10E is a constant, it is a read-only property of Math.

### 例子

The following example displays the base 10 logarithm of E:

document.write("The base 10 logarithm of E is " + Math.LOG10E)

### 相关

• E, LN2, LN10, LOG2E, PI, SQRT1_2, SQRT2 properties
• 阅读全文
0 0