The Location Object

The Location object contains information about the current documents location and can be used to manipulate browser navigation.

For a general example for the location object, consider your browser is currently visiting the rather over-complete URL :

all will become clear.

Location Properties
The hash property returns, or sets the URL fragment part of the current location. For example, from the URL above :


would set the variable vFrag to 'findme'.

The host property sets or returns the host section of the current URL. The host section of a URL is basically the hostname property (see below), together with the port property (see below). So, using the above example :

would set vHost to Note the subtle difference between this and the hostname property. (If the current URL does not contain a port setting, then host and hostname are effectively the same).

The hostname property (as mentioned above) is the name of the host computer (either a name, or IP address). For example :


sets the vHostNane variable to

The href property can be regarded as a concatenation of all the other location properties. That is, if the current URL was the one given above for the example, then :


would make vHref be

The pathname property provides the current path for the URL. This is the section in between the hostname (including protocol) if any and the hash, or search (see below) sections (if either exists). For example :


would return example_files/file.html as the contents of the vPathName variable.

As you may suspect, the port property represents any port settings for the current URL. Given the above example,


sets vPort to 80 (the common port for Web servers).

This property represents the protocol currently in use for the displayed document. For example :


sets vProtocol to 'http:'. For commonly used protocols, see the <A HREF="..."> element.

The search property contains information about any search strings in the current URL. For example :

returns 'Stephen' as the contents of the vSearch variable.

Location Methods
The Internet Explorer 4.0 specific method takes one argument (URL). It then loads the document referenced at the URL (if it exists) in place of the currently displayed document. Basically, it performs the same as changing the href property.

The reload method forces a re-loading of the current URL. It should be noted that Netscape decides how to interact with the server for re-loading according to the Network Preference settings (i.e whether the user has set documents to be verified once, all the time, or never). Saying that however, this decision can be over-ruled by specifying a true parameter. This forces the browser to re-load the current document from the server, despite the current users preference settings. For example :


may re-load the document, depending on user preferences, while :


forces Netscape to re-visit the server to retrieve a new copy of the document.
This can be especially useful if your document changes rapidly, avoiding users seeing old copies of the document due to disk/memory caches, or if they are browsing through a proxy/cache server.

This is much like the reload method, except that it loads another document and replaces the history object entry for the previous URL with the new URL loaded. Essentially, this prevents the user from returning to the previous URL, by using the browsers back button. For example, suppose you are at the URL given above for the examples and the following code gets executed :


The browser would navigate to the Microsoft web site and replace the entry in the history object with Unless you'd remembered the previous URL, there would be no navigating back to it.

Location Events
The Location object has no events.