W3cubDocs

/Web APIs

Location

The Location interface represents the location (URL) of the object it is linked to. Changes done on it are reflected on the object it relates to. Both the Document and Window interface have such a linked Location, accessible via Document.location and Window.location respectively.

Location anatomy

Hover over the URL segments below to highlight their meaning:

Instance properties

Location.ancestorOrigins

A static DOMStringList containing, in reverse order, the origins of all ancestor browsing contexts of the document associated with the given Location object.

Location.href

A stringifier that returns a string containing the entire URL. If changed, the associated document navigates to the new page. It can be set from a different origin than the associated document.

Location.protocol

A string containing the protocol scheme of the URL, including the final ':'.

Location.host

A string containing the host, that is the hostname, a ':', and the port of the URL.

Location.hostname

A string containing the domain of the URL.

Location.port

A string containing the port number of the URL.

Location.pathname

A string containing an initial '/' followed by the path of the URL, not including the query string or fragment.

Location.search

A string containing a '?' followed by the parameters or "querystring" of the URL. Modern browsers provide URLSearchParams and URL.searchParams to make it easy to parse out the parameters from the querystring.

Location.hash

A string containing a '#' followed by the fragment identifier of the URL.

Location.origin Read only

Returns a string containing the canonical form of the origin of the specific location.

Instance methods

Location.assign()

Loads the resource at the URL provided in parameter.

Location.reload()

Reloads the current URL, like the Refresh button.

Location.replace()

Replaces the current resource with the one at the provided URL (redirects to the provided URL). The difference from the assign() method and setting the href property 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.

Location.toString()

Returns a string containing the whole URL. It is a synonym for Location.href, though it can't be used to modify the value.

Examples

js

// location: https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
const loc = document.location;
console.log(loc.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
console.log(loc.protocol); // https:
console.log(loc.host); // developer.mozilla.org:8080
console.log(loc.hostname); // developer.mozilla.org
console.log(loc.port); // 8080
console.log(loc.pathname); // /en-US/search
console.log(loc.search); // ?q=URL
console.log(loc.hash); // #search-results-close-container
console.log(loc.origin); // https://developer.mozilla.org:8080

location.assign("http://another.site"); // load another page

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Location 1 12 1 3 3 1 ≤37 18 4 10.1 1 1.0
ancestorOrigins 20 79 No No 15 6 4.4 25 No 14 6 1.5
assign 1 12 1 5.5 3 3 4.4 18 4 10.1 1 1.0
hash 1 12 1 3 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
host 1 12 1 3 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
hostname 1 12 1 3 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
href 1 12 1 3 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
origin 8 12
21Before Firefox 49, results for URL using the blob scheme incorrectly returned null.
11Intranet sites are set to Compatibility View, which will emulate IE7 and omit window.location.origin.
15 5.1 3 18
21Before Firefox 49, results for URL using the blob scheme incorrectly returned null.
14 5 1.0
pathname 1 12
1Before Firefox 53, the pathname property returned wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" rather than "/x".
3Internet Explorer does not provide the leading slash character in the pathname (docs/Web/API/Location instead of /docs/Web/API/Location).
≤12.1 1 4.4 18
4Before Firefox 53, the pathname property returned wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" rather than "/x".
≤12.1 1 1.0
port 1 12 1 3 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
protocol 1 12 1 3 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
reload 1
12Before Edge 79, if a page added to Trusted Sites contains a cross-origin iframe, then calling reload() from within the iframe reloads the trusted page (in other words, the top page reloads, not the iframe).
1
5.5If a page added to Trusted Sites contains a cross-origin iframe, then calling reload() from within the iframe reloads the trusted page (in other words, the top page reloads, not the iframe).
3 1 4.4 18 4 10.1 1 1.0
replace 1 12 1 5.5 3 1 4.4 18 4 10.1 1 1.0
search 1 12
1Before Firefox 53, the search property returned wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, search would return "", rather than "?a=true&b=false".
3 ≤12.1 1 4.4 18
4Before Firefox 53, the search property returned wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, search would return "", rather than "?a=true&b=false".
≤12.1 1 1.0
toString 52 12 22
11Intranet sites are set to Compatibility View, which will emulate IE7 and omit window.location.toString.
39 1 52 52 22 41 1 6.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Location