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
The Location interface doesn't inherit any property, but implements those from URLUtils.
Location.href- Is a
DOMStringcontaining 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
DOMStringcontaining the protocol scheme of the URL, including the final':'. Location.host- Is a
DOMStringcontaining the host, that is the hostname, a':', and the port of the URL. Location.hostname- Is a
DOMStringcontaining the domain of the URL. Location.port- Is a
DOMStringcontaining the port number of the URL. Location.pathname- Is a
DOMStringcontaining an initial'/'followed by the path of the URL. Location.search- Is a
DOMStringcontaining 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
DOMStringcontaining a'#'followed by the fragment identifier of the URL. Location.username- Is a
DOMStringcontaining the username specified before the domain name. Location.password- Is a
DOMStringcontaining the password specified before the domain name. Location.originRead only- Returns a
DOMStringcontaining the canonical form of the origin of the specific location.
Methods
The Location interface doesn't inherit any method, but implements those from URLUtils.
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
DOMStringcontaining the whole URL. It is a synonym forURLUtils.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/en-US/search?q=URL#search-results-close-container';
console.log(url.href); // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container
console.log(url.protocol); // https:
console.log(url.host); // developer.mozilla.org
console.log(url.hostname); // developer.mozilla.org
console.log(url.port); // (blank - https assumes port 443)
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
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
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
origin on Windows.location |
? | ? | 21 (21) | ? | ? | ? |
origin on all location objects (but on Workers, that use WorkerLocation |
? | ? | 26 (26) | ? | ? | ? |
username and password |
? | ? | 26 (26) | ? | ? | ? |
| Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
origin on Windows.location |
? | ? | 21.0 (21) | ? | ? | ? |
origin on all location objects (but on Workers, that use WorkerLocation) |
? | ? | 26.0 (26) | ? | ? | ? |
username and password |
? | ? | 26.0 (26) | ? | ? | ? |
See also
- Two methods creating such an object:
Window.locationandDocument.location. - URL related interfaces:
URL,URLSearchParamsandHTMLHyperlinkElementUtils
Document Tags and Contributors
Tags:
Contributors to this page:
David_Gilbertson,
Jonathan_Watt,
libbymc,
bakotaco,
Sheppy,
Loadmaster,
teoli,
madarche,
AlexAngas,
fscholz,
kohei.yoshino,
fkling42
Last updated by:
David_Gilbertson,