Table of Contents | Previous
| Next
| Index
Contains information on the current URL.
Location
objects are predefined JavaScript objects that you access through the location
property of a window
object.
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/"
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
, is deprecated.
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.
The following list explains some of the protocols:
In addition, this object inherits the watch
and unwatch
methods from Object
.
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/"
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/"
See also the examples for Anchor
.
History
, document.URL
A string beginning with a hash mark (#) that specifies an anchor name in the URL.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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 252).
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
A string specifying the server name, subdomain, and domain name.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
A string containing the full hostname of the server, including the server name, subdomain, domain, and port number.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
A string specifying the entire URL.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
A string specifying the URL-path portion of the URL.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
A string specifying the communications port that the server uses.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
A string specifying the beginning of the URL, up to and including the first colon.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
Forces a reload of the window's current document (the document specified by the Location.href
property).
reload([forceGet])
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
Loads the specified URL over the current history entry.
replace(URL)
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.
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
A string beginning with a question mark that specifies any query information in the URL.
JavaScript 1.1.
This property is tainted by default. For information on data tainting, see the Client-Side JavaScript Guide.
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
Table of Contents | Previous
| Next
| Index
Last Updated: 05/28/99 11:59:50
Copyright (c) 1999
Netscape Communications Corporation