location | NN 2 IE 3 DOM n/a | |||||||
There is one location object in each window or frame. The object stores all information about the URL of the document currently loaded into that window or frame. By assigning a new URL to the href property of the location object, you instruct the browser to load a new page into the window or frame. This is the primary way of scripting the loading of a new page: location.href = "newPage.html"; |
||||||||
A script in one frame can reference the location object in another frame to load a new document into that other frame: parent.otherFrameName.location.href = "newPage.html"; |
||||||||
Security restrictions prevent a script in one frame from accessing location object information in another frame if the document in the second frame does not come from the same domain (and the same server, unless the document.domain properties of the two documents are set to match) as the document with the nosy script. This prevents a rogue script from monitoring navigation in another frame to external web sites. In Navigator 4 and later, you can overcome the security restriction with the help of signed scripts, but the user still has to give explicit permission for a script to access location object information outside the script's domain. |
||||||||
As a window-related object, the location object is not part of the formal W3C DOM Level 1 or 2 specifications (which leave windows for future versions). But the location object and its properties are well-entrenched in scripting vernacular, and should continue to be supported for a long time coming. |
||||||||
Object Model Reference | ||||||||
[windowRef.]location
|
||||||||
Object-Specific Properties | ||||||||
|
||||||||
Object-Specific Methods | ||||||||
|
||||||||
Object-Specific Event Handler Properties | ||||||||
None. |
hash | NN 2 IE 3 DOM n/a |
Read/Write | |
Indicates that portion of a URL following the # symbol, referring to an anchor location in a document. This property contains its data only if the user has explicitly navigated to an anchor, and is not just scrolling to it. Do not include the # symbol when setting the property. |
|
Example | |
location.hash = "section3"; |
|
Value | |
String. |
|
Default | |
None. |
host | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the combination of the hostname and port (if any) of the server that serves up the current document. If the port is explicitly part of the URL, the hostname and port are separated by a colon, just as they are in the URL. |
|
Example | |
if (location.host == "www.megacorp.com:80") { ... } |
|
Value | |
String of hostname, optionally followed by a colon and port number. |
|
Default | |
Depends on server. |
hostname | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the combination of the hostname of the server (i.e., a two-dot address consisting of server name and domain) that serves up the current document. The hostname property does not include the port number. |
|
Example | |
if (location.hostname == "www.megacorp.com") { ... } |
|
Value | |
String of hostname (server and domain). |
|
Default | |
Depends on server. |
href | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the complete URL of the document loaded in the window or frame. Assigning a URL to this property is how you script navigation to load a new document into the window or frame (although Internet Explorer also offers the equivalent window.navigate( ) method). |
|
Example | |
location.href = "http://www.megacorp.com"; |
|
Value | |
String of complete or relative URL. |
|
Default | |
None. |
pathname | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the pathname component of the URL. This consists of all URL information following the last character of the domain name, including the initial forward slash symbol. |
|
Example | |
location.pathname = "/images/logoHiRes.gif"; |
|
Value | |
String. |
|
Default | |
None. |
port | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the port component of the URL, if one exists. This consists of all URL information following the colon after the last character of the domain name. The colon is not part of the port property value. |
|
Example | |
location.port = "80"; |
|
Value | |
String (a numeric value as string). |
|
Default | |
None. |
protocol | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the protocol component of the URL. This consists of all URL information up to and including the first colon of a URL. Typical values are: "http:", "file:", "ftp:", and "mailto:". |
|
Example | |
if (location.protocol == "file:") { // statements for treating document as local file } |
|
Value | |
String. |
|
Default | |
None. |
search | NN 2 IE 3 DOM n/a |
Read/Write | |
Provides the URL-encoded portion of a URL that begins with the ? symbol. A document that is served up as the result of the search also may have the search portion available as part of the window.location property. You can modify this property by script. Doing so sends the URL and search criteria to the server. You must know the format of data (usually name/value pairs) expected by the server to perform this properly. You can also pass string data between separate pages by appending a search string to the next page's URL. While the search string appendage does not affect retrieval of the page, the string arrives with the new page in the new page's location object. A script in the new page can read and dissect the location.search property to place the passed values in variables that scripts in the page may use for their processing. |
|
Example | |
location.search="?p=Tony+Blair&d=y&g=0&s=a&w=s&m=25"; |
|
Value | |
String starting with the ? symbol. |
|
Default | |
None. |
assign( ) | NN 2 IE 3 DOM n/a |
assign("URL") | |
This method was intended to be hidden from view of scripters, but remains available for now. It performs the same action as assigning a URL to the location.href property. The assign( ) method is listed here for completeness and should not be used. |
|
Parameters | |
|
|
Returned Value | |
None. |
reload( ) | NN 3 IE 4 DOM n/a |
reload([unconditional]) | |
Performs a hard reload of the document associated with the location object. This kind of reload resets form elements to their default values (for a soft reload, use history.go(0)). By default, the reload( ) method performs a conditional-get action, which retrieves the file from the browser cache if the file is still in the cache (and the cache is turned on). To force a reload from the server, force an unconditional-get by adding the true Boolean parameter. |
|
Parameters | |
|
|
Returned Value | |
None. |
replace( ) | NN 3 IE 4 DOM n/a |
replace("URL") | |
Loads a new document into the reference window and replaces the browser's history listing entry of the current document with the entry of the new document. Thus, some interim page that you don't want appearing in history (to prevent the Back button from ever returning to the page) can be removed from the history and replaced with the entry of the newly loaded document. |
|
Parameters | |
|
|
Returned Value | |
None. |