JavaScript语言参考手册      
JavaScript手册
【目录】 【上一页】 【下一页】 【索引】

Location

包含了关于当前 URL 的信息。

客户端对象
实现版本 Navigator 2.0
Navigator 3.0: 添加了 reload, replace 方法

创建源

Location 对象是预先定义的 JavaScript 对象,你可以通过一个 Window 对象的 location 属性访问。

描述

location 对象描述了与一个给定的 Window 对象关联的完整 URL。location 对象的每个属性都描述了 URL 的不同特性。

通常情况下,一个 URL 会有下面的格式:

协议//主机:端口/路径名称#哈希标识?搜索条件 例如:

http://home.netscape.com/assist/extensions.html#topic1?x=7&y=2 这些部分是满足下列需求的:

A Location object has a property for each of these parts of the URL. See the individual properties for more information. A Location object has two other properties not shown here:

If you assign a string to the location property of an object, JavaScript creates a location object and assigns that string to its href property. For example, the following two statements are equivalent and set the URL of the current window to the Netscape home page:

window.location.href="http://home.netscape.com/"
window.location="http://home.netscape.com/"
The location object is contained by the window object and is within its scope. If you refer to a location object without specifying a window, the location object represents the current location. If you refer to a location object and specify a window name, as in windowReference.location, the location object represents the location of the specified window.

In event handlers, you must specify window.location instead of simply using location. Due to the scoping of static objects in JavaScript, a call to location without specifying an object name is equivalent to document.location, which is a synonym for document.URL.

Location is not a property of the document object; its equivalent is the document.URL property. The document.location property, which is a synonym for document.URL, will be removed in a future release.

How documents are loaded when location is set

When you set the location object or any of its properties except hash, whether a new document is loaded depends on which version of the browser you are running:

语法 for common URL types

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

表 6.2 URL语法.  
URL type Protocol Example
JavaScript code javascript: javascript:history.go(-1)
Navigator source viewer view-source: view-source:wysiwyg://0/file:/c|/temp/genhtml.html
Navigator info about: about:cache
World Wide Web http: http://home.netscape.com/
File file:/ file:///javascript/方法s.html
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 (by calling a void function, for example javascript:void(0)), no new page loads. Note that loading a new page over your script's page clears the page's variables, functions, and so on.

The view-source: protocol displays HTML code that was generated with JavaScript document.write and document.writeln methods. For information on printing and saving generated HTML, see write.

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

about:
about:cache
about:plugins

属性概览

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

方法概览

reload Forces a reload of the window's current document.
replace Loads the specified URL over the current history entry.

示例

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

window.location.href="http://home.netscape.com/"
window.location="http://home.netscape.com/"
示例 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/" See also the示例 for Anchor.

参看

History, document.URL

属性

hash

A string beginning with a hash mark (#) that specifies an anchor name in the URL.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The hash property specifies a portion of the URL. This property applies to HTTP URLs only.

You can set the hash property at any time, although it is safer to set the href property to change a location. If the hash that you specify cannot be found in the current location, you get an error.

Setting the hash property navigates to the named anchor without reloading the document. This differs from the way a document is loaded when other location properties are set (see "How documents are loaded when location is set").

See RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the hash.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.hash = " +
   newWindow.location.hash + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.hash = #checkbox_object

参看

Location.host, Location.hostname, Location.href, Location.pathname, Location.port, Location.protocol, Location.search

host

A string specifying the server name, subdomain, and domain name.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The host property specifies a portion of a URL. The host 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 null, the host property is the same as the hostname property.

You can set the host property at any time, although it is safer to set the href property to change a location. If the host that you specify cannot be found in the current location, you 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 hostname and port.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.host = " +
   newWindow.location.host + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.host = home.netscape.com

参看

Location.hash, Location.hostname, Location.href, Location.pathname, Location.port, Location.protocol, Location.search

hostname

A string containing the full hostname of the server, including the server name, subdomain, domain, and port number.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The hostname property specifies a portion of a URL. 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 hostname property at any time, although it is safer to set the href property to change a location. If the hostname that you specify cannot be found in the current location, you 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 hostname.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.hostName = " +
   newWindow.location.hostName + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.hostName = home.netscape.com

参看

Location.hash, Location.host, Location.href, Location.pathname, Location.port, Location.protocol, Location.search

href

A string specifying the entire URL.

属性源 Location
实现版本 Navigator 2.0
<


Location

包含了关于当前 URL 的信息。

客户端对象
实现版本 Navigator 2.0
Navigator 3.0: 添加了 reload, replace 方法

创建源

Location 对象是预先定义的 JavaScript 对象,你可以通过一个 Window 对象的 location 属性访问。

描述

location 对象描述了与一个给定的 Window 对象关联的完整 URL。location 对象的每个属性都描述了 URL 的不同特性。

通常情况下,一个 URL 会有下面的格式:

协议//主机:端口/路径名称#哈希标识?搜索条件 例如:

http://home.netscape.com/assist/extensions.html#topic1?x=7&y=2 这些部分是满足下列需求的:

A Location object has a property for each of these parts of the URL. See the individual properties for more information. A Location object has two other properties not shown here:

If you assign a string to the location property of an object, JavaScript creates a location object and assigns that string to its href property. For example, the following two statements are equivalent and set the URL of the current window to the Netscape home page:

window.location.href="http://home.netscape.com/"
window.location="http://home.netscape.com/"
The location object is contained by the window object and is within its scope. If you refer to a location object without specifying a window, the location object represents the current location. If you refer to a location object and specify a window name, as in windowReference.location, the location object represents the location of the specified window.

In event handlers, you must specify window.location instead of simply using location. Due to the scoping of static objects in JavaScript, a call to location without specifying an object name is equivalent to document.location, which is a synonym for document.URL.

Location is not a property of the document object; its equivalent is the document.URL property. The document.location property, which is a synonym for document.URL, will be removed in a future release.

How documents are loaded when location is set

When you set the location object or any of its properties except hash, whether a new document is loaded depends on which version of the browser you are running:

语法 for common URL types

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

表 6.2 URL语法.  
URL type Protocol Example
JavaScript code javascript: javascript:history.go(-1)
Navigator source viewer view-source: view-source:wysiwyg://0/file:/c|/temp/genhtml.html
Navigator info about: about:cache
World Wide Web http: http://home.netscape.com/
File file:/ file:///javascript/方法s.html
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 (by calling a void function, for example javascript:void(0)), no new page loads. Note that loading a new page over your script's page clears the page's variables, functions, and so on.

The view-source: protocol displays HTML code that was generated with JavaScript document.write and document.writeln methods. For information on printing and saving generated HTML, see write.

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

about:
about:cache
about:plugins

属性概览

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

方法概览

reload Forces a reload of the window's current document.
replace Loads the specified URL over the current history entry.

示例

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

window.location.href="http://home.netscape.com/"
window.location="http://home.netscape.com/"
示例 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/" See also the示例 for Anchor.

参看

History, document.URL

属性

hash

A string beginning with a hash mark (#) that specifies an anchor name in the URL.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The hash property specifies a portion of the URL. This property applies to HTTP URLs only.

You can set the hash property at any time, although it is safer to set the href property to change a location. If the hash that you specify cannot be found in the current location, you get an error.

Setting the hash property navigates to the named anchor without reloading the document. This differs from the way a document is loaded when other location properties are set (see "How documents are loaded when location is set").

See RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the hash.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.hash = " +
   newWindow.location.hash + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.hash = #checkbox_object

参看

Location.host, Location.hostname, Location.href, Location.pathname, Location.port, Location.protocol, Location.search

host

A string specifying the server name, subdomain, and domain name.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The host property specifies a portion of a URL. The host 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 null, the host property is the same as the hostname property.

You can set the host property at any time, although it is safer to set the href property to change a location. If the host that you specify cannot be found in the current location, you 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 hostname and port.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.host = " +
   newWindow.location.host + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.host = home.netscape.com

参看

Location.hash, Location.hostname, Location.href, Location.pathname, Location.port, Location.protocol, Location.search

hostname

A string containing the full hostname of the server, including the server name, subdomain, domain, and port number.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The hostname property specifies a portion of a URL. 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 hostname property at any time, although it is safer to set the href property to change a location. If the hostname that you specify cannot be found in the current location, you 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 hostname.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.hostName = " +
   newWindow.location.hostName + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.hostName = home.netscape.com

参看

Location.hash, Location.host, Location.href, Location.pathname, Location.port, Location.protocol, Location.search

href

A string specifying the entire URL.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The href property specifies the entire URL. Other location object properties are substrings of the href property. If you want to change the URL associated with a window, you should do so by changing the href property; this correctly updates all of the other properties.

You can set the href property at any time.

Omitting a property name from the location object is equivalent to specifying location.href. For example, the following two statements are equivalent and set the URL of the current window to the Netscape home page:

window.location.href="http://home.netscape.com/"
window.location="http://home.netscape.com/"
See RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the URL.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display all the properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.protocol = " +
   newWindow.location.protocol + "<P>")
msgWindow.document.write("newWindow.location.host = " +
   newWindow.location.host + "<P>")
msgWindow.document.write("newWindow.location.hostName = " +
   newWindow.location.hostName + "<P>")
msgWindow.document.write("newWindow.location.port = " +
   newWindow.location.port + "<P>")
msgWindow.document.write("newWindow.location.pathname = " +
   newWindow.location.pathname + "<P>")
msgWindow.document.write("newWindow.location.hash = " +
   newWindow.location.hash + "<P>")
msgWindow.document.write("newWindow.location.search = " +
   newWindow.location.search + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.protocol = http:
newWindow.location.host = home.netscape.com
newWindow.location.hostName = home.netscape.com
newWindow.location.port =
newWindow.location.pathname =
   /comprod/products/navigator/version_2.0/script/
   script_info/objects.html
newWindow.location.hash = #checkbox_object
newWindow.location.search =

参看

Location.hash, Location.host, Location.hostname, Location.pathname, Location.port, Location.protocol, Location.search

pathname

A string specifying the URL-path portion of the URL.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

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 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.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.pathname = " +
   newWindow.location.pathname + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.pathname =
   /comprod/products/navigator/version_2.0/script/
   script_info/objects.html

参看

Location.hash, Location.host, Location.hostname, Location.href, Location.port, Location.protocol, Location.search

port

A string specifying the communications port that the server uses.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

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.

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 get an error. If the port property is not specified, it defaults to 80.

See Section 3.1 of RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the port.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.port = " +
   newWindow.location.port + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.port =

参看

Location.hash, Location.host, Location.hostname, Location.href, Location.pathname, Location.protocol, Location.search

protocol

A string specifying the beginning of the URL, up to and including the first colon.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The protocol property specifies a portion of the URL. The protocol indicates the access method of the URL. For example, the value "http:" specifies HyperText Transfer Protocol, and the value "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 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.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.write("newWindow.location.protocol = " +
   newWindow.location.protocol + "<P>")
msgWindow.document.close()
The previous example displays output such as the following:

newWindow.location.href =
   http://home.netscape.com/comprod/products/navigator/
   version_2.0/script/script_info/objects.html#checkbox_object
newWindow.location.protocol = http:

参看

Location.hash, Location.host, Location.hostname, Location.href, Location.pathname, Location.port, Location.search

search

A string beginning with a question mark that specifies any query information in the URL.

属性源 Location
实现版本 Navigator 2.0

安全性

Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”

描述

The search property specifies a portion of the URL. This property applies to HTTP URLs only.

The search property contains variable and value pairs; each pair is separated by an ampersand. For example, two pairs in a search string could look as follows:

?x=7&y=5 You can set the search property at any time, although it is safer to set the href property to change a location. If the search that you specify cannot be found in the current location, you get an error.

See Section 3.3 of RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the search.

示例

In the following example, the window.open statement creates a window called newWindow and loads the specified URL into it. The document.write statements display properties of newWindow.location in a window called msgWindow.

newWindow=window.open
   ("http://guide-p.infoseek.com/WW/NS/Titles?qt=RFC+1738+&col=WW")
msgWindow.document.write("newWindow.location.href = " +
   newWindow.location.href + "<P>")
msgWindow.document.close()
msgWindow.document.write("newWindow.location.search = " +
   newWindow.location.search + "<P>")
msgWindow.document.close()
The previous example displays the following output:

newWindow.location.href =
   http://guide-p.infoseek.com/WW/NS/Titles?qt=RFC+1738+&col=WW
newWindow.location.search = ?qt=RFC+1738+&col=WW

参看

Location.hash, Location.host, Location.hostname, Location.href, Location.pathname, Location.port, Location.protocol

方法

reload

Forces a reload of the window's current document (the document specified by the Location.href property).

方法源 Location
实现版本 Navigator 3.0

语法

reload(forceGet)

参数

forceGet (Optional) If you supply true, forces an unconditional HTTP GET of the document from the server. This should not be used unless you have reason to believe that disk and memory caches are off or broken, or the server has a new version of the document (for example, if it is generated by a CGI on each request).

描述

This method uses the same policy that the browser's Reload button uses. The user interface for setting the default value of this policy varies for different browser versions.

By default, the reload method does not force a transaction with the server. However, if the user has set the preference to check every time, the method does a "conditional GET" request using an If-modified-since HTTP header, to ask the server to return the document only if its last-modified time is newer than the time the client keeps in its cache. In other words, reload reloads from the cache, unless the user has specified to check every time and the document has changed on the server since it was last loaded and saved in the cache.

示例

The following example displays an image and three radio buttons. The user can click the radio buttons to choose which image is displayed. Clicking another button lets the user reload the document.

<SCRIPT>
function displayImage(theImage) {
   document.images[0].src=theImage
}
</SCRIPT>
<FORM NAME="imageForm">
<B>Choose an image:</B>
<BR><INPUT TYPE="radio" NAME="imageChoice" VALUE="image1" CHECKED
   onClick="displayImage('seaotter.gif')">Sea otter
<BR><INPUT TYPE="radio" NAME="imageChoice" VALUE="image2"
   onClick="displayImage('orca.gif')">Killer whale
<BR><INPUT TYPE="radio" NAME="imageChoice" VALUE="image3"
   onClick="displayImage('humpback.gif')">Humpback whale
<BR>
<IMG NAME="marineMammal" SRC="seaotter.gif" ALIGN="left" VSPACE="10">
<P><INPUT TYPE="button" VALUE="Click here to reload"
   onClick="window.location.reload()">
</FORM>

参看

Location.replace

replace

Loads the specified URL over the current history entry.

方法源 Location
实现版本 Navigator 3.0

语法

replace("URL")

参数

URL Specifies the URL to load.

描述

The replace method loads the specified URL over the current history entry. After calling the replace method, the user cannot navigate to the previous URL by using browser's Back button.

If your program will be run with JavaScript in Navigator 2.0, you could put the following line in a SCRIPT tag early in your program. This emulates replace, which was introduced in Navigator 3.0:

if (location.replace == null)
   location.replace = location.assign
The replace method does not create a new entry in the history list. To create an entry in the history list while loading a URL, use the History.go method.

示例

The following example lets the user choose among several catalogs to display. The example displays two sets of radio buttons which let the user choose a season and a category, for example the Spring/Summer Clothing catalog or the Fall/Winter Home & Garden catalog. When the user clicks the Go button, the displayCatalog function executes the replace method, replacing the current URL with the URL appropriate for the catalog the user has chosen. After invoking displayCatalog, the user cannot navigate to the previous URL (the list of catalogs) by using browser's Back button.

<SCRIPT>
function displayCatalog() {
   var seaName=""
   var catName=""
   for (var i=0; i < document.catalogForm.season.length; i++) {
      if (document.catalogForm.season[i].checked) {
         seaName=document.catalogForm.season[i].value
         i=document.catalogForm.season.length
      }
   }
   for (var i in document.catalogForm.category) {
      if (document.catalogForm.category[i].checked) {
         catName=document.catalogForm.category[i].value
         i=document.catalogForm.category.length
      }
   }
   fileName=seaName + catName + ".html"
   location.replace(fileName)
}
</SCRIPT>
<FORM NAME="catalogForm">
<B>Which catalog do you want to see?</B>
<P><B>Season</B>
<BR><INPUT TYPE="radio" NAME="season" VALUE="q1" CHECKED>Spring/Summer
<BR><INPUT TYPE="radio" NAME="season" VALUE="q3">Fall/Winter
<P><B>Category</B>
<BR><INPUT TYPE="radio" NAME="category" VALUE="clo" CHECKED>Clothing
<BR><INPUT TYPE="radio" NAME="category" VALUE="lin">Linens
<BR><INPUT TYPE="radio" NAME="category" VALUE="hom">Home & Garden
<P><INPUT TYPE="button" VALUE="Go" onClick="displayCatalog()">
</FORM>

参看

History, Window.open, History.go, Location.reload


【目录】 【上一页】 【下一页】 【索引】

返回页面顶部

JavaScript中文手册 站长在线 整理