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.
Properties
Location.href- Is a stringifier that returns a
USVStringcontaining 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- Is a
USVStringcontaining the protocol scheme of the URL, including the final':'. Location.host- Is a
USVStringcontaining the host, that is the hostname, a':', and the port of the URL. Location.hostname- Is a
USVStringcontaining the domain of the URL. Location.port- Is a
USVStringcontaining the port number of the URL. Location.pathname- Is a
USVStringcontaining an initial'/'followed by the path of the URL. Location.search- Is a
USVStringcontaining 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- Is a
USVStringcontaining a'#'followed by the fragment identifier of the URL. Location.username- Is a
USVStringcontaining the username specified before the domain name. Location.password- Is a
USVStringcontaining the password specified before the domain name. Location.originRead only- Returns a
USVStringcontaining the canonical form of the origin of the specific location.
Methods
Location.assign()- Loads the resource at the URL provided in parameter.
Location.reload()- Reloads the resource from the current URL. Its optional unique parameter is a
Boolean, which, when it istrue, causes the page to always be reloaded from the server. If it isfalseor not specified, the browser may reload the page from its cache. Location.replace()- Replaces the current resource with the one at the provided URL. The difference from the
assign()method is that after usingreplace()the current page will not be saved in sessionHistory, meaning the user won't be able to use the back button to navigate to it. Location.toString()- Returns a
USVStringcontaining the whole URL. It is a synonym forHTMLHyperlinkElementUtils.href, though it can't be used to modify the value.
Examples
// Create anchor element and use href property for the purpose of this example
// A more correct alternative is to browse to the URL and use document.location or window.location
var url = document.createElement('a');
url.href = 'https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container';
console.log(url.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
console.log(url.protocol); // https:
console.log(url.host); // developer.mozilla.org:8080
console.log(url.hostname); // developer.mozilla.org
console.log(url.port); // 8080
console.log(url.pathname); // /en-US/search
console.log(url.search); // ?q=URL
console.log(url.hash); // #search-results-close-container
console.log(url.origin); // https://developer.mozilla.org:8080
Specifications
| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'Location' in that specification. |
Living Standard | No change from HTML5. |
| HTML5 The definition of 'Location' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Location | Chrome Full support Yes | Edge Full support Yes | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android ? | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
assign | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
hash | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 22 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 22 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
host | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 22 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 22 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
hostname | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 22 | IE Full support Yes | Opera No support No | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 22 | Opera Android No support No | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
href | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 22 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 22 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
origin | Chrome Full support Yes | Edge Full support 12 | Firefox
Full support
26
| IE
Full support
11
| Opera Full support 10 | Safari
Full support
Yes
| WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
26
| Opera Android ? | Safari iOS Full support 5 | Samsung Internet Android ? |
password | Chrome Full support Yes | Edge ? | Firefox Full support 26 | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 26 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
pathname | Chrome Full support Yes | Edge Full support 12 | Firefox
Full support
22
| IE
Full support
Yes
| Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
22
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
port | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 22 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 22 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
protocol | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 22 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 22 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
reload | Chrome Full support Yes | Edge
Full support
12
| Firefox Full support Yes | IE
Full support
Yes
| Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
replace | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
search | Chrome Full support Yes | Edge Full support 12 | Firefox
Full support
22
| IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
22
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
toString | Chrome Full support 52 | Edge Full support Yes | Firefox Full support 22 | IE
Full support
11
| Opera ? | Safari ? | WebView Android Full support 52 | Chrome Android Full support 52 | Firefox Android Full support 22 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
username | Chrome Full support Yes | Edge ? | Firefox Full support 26 | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 26 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.
See also
- Two methods creating such an object:
Window.locationandDocument.location. - URL related interfaces:
URL,URLSearchParamsandHTMLHyperlinkElementUtils