locationNN 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
 
hashhosthostnamehrefpathnameportprotocolsearch
 
Object-Specific Methods
 
assign( )reload( )replace( )
 
Object-Specific Event Handler Properties

None.

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

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

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

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

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

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

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

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
 
  • A string version of a complete or relative URL of a document to be loaded into a window or frame.
 
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
 
  • An optional Boolean value. If true, the browser performs an unconditional-get to force a reload of the document from the server, rather than the browser cache.
 
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
 
  • A string version of a complete or relative URL of a document to be loaded into a window or frame.
 
Returned Value

None.