location Location - Window |
Summary
Returns a Location
object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.
Syntax
var locationObj = window.location; window.location = newLocation;
where
- locationObj is an object of type
Location
, providing information about the current URL and methods to change it. Its properties and methods are described below. - newLocation is a
Location
object or a string, specifying the URL to navigate to.
Location
object
Location
objects have a toString
method returning the current URL. You can also assign a string to document.location
. This means that you can work with document.location
as if it were a string in most cases. Sometimes, for example when you need to call a String method on it, you have to explicitly call toString
:
alert(document.location.toString().charAt(17))
Properties
All of the following properties are strings. You can read them to get information about the current URL or set them to navigate to another URL.
The "Example" column contains the values of the properties of the following URL:
- http://www.google.com:80/search?q=devmo#test
Property | Description | Example |
---|---|---|
hash | the part of the URL that follows the # symbol. | #test |
host | the host name and port number. | www.google.com:80 |
hostname | the host name (without the port number). | www.google.com |
href | the entire URL. | http://www.google.com:80/search?q=devmo#test |
pathname | the path (relative to the host). | /search |
port | the port number of the URL. | 80 |
protocol | the protocol of the URL. | http: |
search | the part of the URL that follows the?? symbol, including the?? symbol. | ?q=devmo |
If the hash part of the URL contains encoded characters (see Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent), hash
returns the decoded URL part. This is a bug in Firefox. href
, search
and pathname
return the correct, encoded URL parts. For example:
- http://www.google.com/search?q=Fire%20fox#Fire%20fox
results in:
- hash=#Fire fox
- search=?q=Fire%20fox
Methods
Method | Description |
---|---|
assign(url) | Load the document at the provided URL. |
reload(forceget) | Reload the document from the current URL. forceget is a boolean, which, when it is true , causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache. |
replace(url) | Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it. |
toString() | Returns the string representation of the Location object's URL. See the JavaScript reference for details. |
Example
Whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign()
had been called with the modified URL.
Replace the current document with the one at the given URL:
function goMoz() { window.location = "http://www.mozilla.org"; } // in html: <button οnclick="goMoz();">Mozilla</button>
Display the properties of the current URL in an alert dialog:
function showLoc() { var x = window.location; var t = ['Property - Typeof - Value', 'window.location - ' + (typeof x) + ' - ' + x ]; for (var prop in x){ t.push(prop + ' - ' + (typeof x[prop]) + ' - ' + (x[prop] || 'n/a')); } alert(t.join('\n')); } // in html: <button οnclick="showLoc();">Show location properties</button>
Send a string of data to the server by modifying the search
property:
function sendData(dat) { window.location.search = dat; } // in html: <button οnclick="sendData('Some data');">Send data</button>
The current URL with "?Some%20data" appended is sent to the server (if no action is taken by the server, the current document is reloaded with the modified search string).
Specification
DOM Level 0. Not part of any standard.
- Content is available under these licenses.
- About MDC
@compat
- IE6|IE7|FF1|FF2|FF3|OPERA|SAFARI2|SAFARI3|KONQ