This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
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.
Hover over the URL segments below to highlight their meaning:
Location.ancestorOrigins Read only
A static DOMStringList containing, in reverse order, the origins of all ancestor browsing contexts of the document associated with the given Location object.
Location.hrefA 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.protocolA string containing the protocol scheme of the URL, including the final ':'.
Location.hostA string containing the host, that is the hostname, a ':', and the port of the URL.
Location.hostnameA string containing the domain of the URL.
Location.portA string containing the port number of the URL.
Location.pathnameA string containing an initial '/' followed by the path of the URL, not including the query string or fragment.
Location.searchA string containing a '?' followed by the parameters or "query string" of the URL. Modern browsers provide URLSearchParams and URL.searchParams to make it easy to parse out the parameters from the query string.
Location.hashA 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.
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.
// 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
| Specification |
|---|
| HTML> # the-location-interface> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
Location |
1 | 12 | 1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 3 | 1 |
ancestorOrigins |
20 | 79 | No | 15 | 6 | 25 | No | 14 | 6 | 1.5 | 4.4 | 6 |
assign |
1 | 12 | 1 | 3 | 3 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 |
hash |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
host |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
hostname |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
href |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
origin |
8 | 12 | 21Before Firefox 49, results for URL using the blob scheme incorrectly returned null. |
15 | 5.1 | 18 | 21Before Firefox for Android 49, results for URL using the blob scheme incorrectly returned null. |
14 | 5 | 1.0 | 3 | 5 |
pathname |
1 | 12 | 1Before Firefox 53, thepathname property returned wrong parts of the URL. For example, for a URL of https://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" rather than "/x". |
≤12.1 | 1 | 18 | 4Before Firefox for Android 53, thepathname property returned wrong parts of the URL. For example, for a URL of https://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" rather than "/x". |
≤12.1 | 1 | 1.0 | 4.4 | 1 |
port |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
protocol |
1 | 12 | 1 | ≤12.1 | 1 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
reload |
1 | 12Before Edge 79, if a page added to Trusted Sites contains a cross-origin iframe, then callingreload() from within the iframe reloads the trusted page (in other words, the top page reloads, not the iframe). |
1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 |
replace |
1 | 12 | 1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 |
search |
1 | 12 | 1Before Firefox 53, thesearch property returned wrong parts of the URL. For example, for a URL of https://z.com/x?a=true&b=false, search would return "", rather than "?a=true&b=false". |
≤12.1 | 1 | 18 | 4Before Firefox for Android 53, thesearch property returned wrong parts of the URL. For example, for a URL of https://z.com/x?a=true&b=false, search would return "", rather than "?a=true&b=false". |
≤12.1 | 1 | 1.0 | 4.4 | 1 |
toString |
52 | 12 | 22 | 39 | 1 | 52 | 22 | 41 | 1 | 6.0 | 52 | 1 |
Location properties: Window.location and Document.location.URL and URLSearchParams.
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Location