This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

window.location

Summary

The location object provides access to the address related properties of the current document.

Properties

hash
The hash property contains the fragment identifier (including hash character) for the current page.
host
The host property contains the host part of the the current document URL, (hostname:port).
hostname
The hostname property contains the hostname the current document was served from, excluding protocol, port, and other information.
href
The full url for this resource. Synonymous with String(window.location).
pathname
The path to the current document, relative to the root of the current host.
port
The port number the document was accessed via.
protocol
Sets or retrieves the protocol portion of a URL. The protocol the current document was accessed via (everything preceding the “//”).
search
The search property contains the query string portion of the current url.

Methods

assign
Navigate to a new page.
reload
Refresh/reload the current page, optionally forcing a re-download of the content.
replace
Similar to window.location.assign, except that it “replaces” the current document, removing the previous one from the back button history.

Events

No events.

Examples

Redirect the current page to example.org.

window.location = "http://example.org";

Show an alert if the current page is example.org.

if(window.location == "http://example.org/"){
    alert("Welcome to example.org!");
}

Usage

 The location object can be used as an object or a string.

Regular string comparisons such as in the examples section can be used, but there are several other properties of the location such as the hash or the hostname that can be compared individually.