Полезная информация

Table of Contents | Previous | Next | Index


Location

Contains information on the current URL.

Client-side object

Implemented in

JavaScript 1.0

JavaScript 1.1: added reload, replace methods

Created by

Location objects are predefined JavaScript objects that you access through the location property of a window object.

Description

The location object represents the complete URL associated with a given window object. Each property of the location object represents a different portion of the URL.

In general, a URL has this form:

protocol//host:port/pathname#hash?search
For example:

http://home.netscape.com/assist/extensions.html#topic1?x=7&y=2
These parts serve the following purposes:

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/" rel="nofollow" 
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:

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

Table 1.1 URL syntax.  
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/methods.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 document.write.

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

about:
about:cache
about:plugins

Property Summary

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

Method Summary

Method Description
reload

Forces a reload of the window's current document.

replace

Loads the specified URL over the current history entry.

In addition, this object inherits the watch and unwatch methods from Object.

Examples

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

See also

History, document.URL


hash

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

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description

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" on page 236).

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

Examples

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

See also

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.

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description

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.

Examples

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

See also

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.

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description

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.

Examples

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

See also

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


href

A string specifying the entire URL.

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description

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/" rel="nofollow" 
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.

Examples

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 =

See also

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.

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

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

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

See also

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.

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

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.

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.

Examples

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 =

See also

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.

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description

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.

Examples

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:

See also

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


reload

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

Method of

Location

Implemented in

JavaScript 1.1

Syntax

reload([forceGet])

Parameters

forceGet

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

Description

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.

Examples

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>

See also

Location.replace


replace

Loads the specified URL over the current history entry.

Method of

Location

Implemented in

JavaScript 1.1

Syntax

replace(URL)

Parameters

URL

A string specifying the URL to load.

Description

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 1.0, you could put the following line in a SCRIPT tag early in your program. This emulates replace, which was introduced in JavaScript 1.1:

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.

Examples

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>

See also

History, window.open, History.go, Location.reload


search

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

Property of

Location

Implemented in

JavaScript 1.0

Security

JavaScript 1.1: This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.

Description

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.

Examples

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

See also

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


Table of Contents | Previous | Next | Index

Last Updated: 11/16/98 12:56:42

Copyright (c) 1998 Netscape Communications Corporation