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.

Anatomy Of Location

HTML

<span id="href" title="href"><span id="protocol" title="protocol">http:</span>//<span id="host" title="host"><span id="hostname" title="hostname">example.org</span>:<span id="port" title="port">8888</span></span><span id="pathname" title="pathname">/foo/bar</span><span id="search" title="search">?q=baz</span><span id="hash" title="hash">#bang</span></span>

CSS

html, body {height:100%;}
html {display:table; width:100%;}
body {display:table-cell; text-align:center; vertical-align:middle; font-family:georgia; font-size:230%; line-height:1em; white-space:nowrap;}

[title] {position:relative; display:inline-block; box-sizing:border-box; /*border-bottom:.5em solid;*/ line-height:2em; cursor:pointer;}

[title]:before {content:attr(title); font-family:monospace; position:absolute; top:100%; width:100%; left:50%; margin-left:-50%; font-size:40%; line-height:1.5; background:black;}
[title]:hover:before,
:target:before {background:black; color:yellow;}

[title] [title]:before {margin-top:1.5em;}
[title] [title] [title]:before {margin-top:3em;}

[title]:hover,
:target {position:relative; z-index:1; outline:50em solid rgba(255,255,255,.8);}

JavaScript

[].forEach.call(document.querySelectorAll('[title][id]'), function (node) {
  node.addEventListener("click", function(e) {
    e.preventDefault();
    e.stopPropagation();
    window.location.hash = '#' + $(this).attr('id');
  });
});
[].forEach.call(document.querySelectorAll('[title]'), function (node) {
  node.addEventListener("click", function(e) {
    e.preventDefault();
    e.stopPropagation();
    window.location.hash = '';
  });
});

Result

Properties

Location.href
Is a stringifier that returns a USVString 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
Is a USVString containing the protocol scheme of the URL, including the final ':'.
Location.host
Is a USVString containing the host, that is the hostname, a ':', and the port of the URL.
Location.hostname
Is a USVString containing the domain of the URL.
Location.port
Is a USVString containing the port number of the URL.
Location.pathname
Is a USVString containing an initial '/' followed by the path of the URL.
Location.search
Is a USVString 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
Is a USVString containing a '#' followed by the fragment identifier of the URL.
Location.origin Read only
Returns a USVString containing 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 is true, causes the page to always be reloaded from the server. If it is false or 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 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 USVString containing the whole URL. It is a synonym for HTMLHyperlinkElementUtils.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

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
LocationChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
assignChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 3Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
hashChrome Full support YesEdge Full support 12Firefox Full support 22IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
hostChrome Full support YesEdge Full support 12Firefox Full support 22IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
hostnameChrome Full support YesEdge Full support 12Firefox Full support 22IE Full support YesOpera No support NoSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android No support NoSafari iOS Full support YesSamsung Internet Android Full support Yes
hrefChrome Full support YesEdge Full support 12Firefox Full support 22IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
originChrome Full support YesEdge Full support 12Firefox Full support 26
Notes
Full support 26
Notes
Notes Before Firefox 49, results for URL using the blob scheme incorrectly returned null.
IE Full support 11
Notes
Full support 11
Notes
Notes Intranet sites are set to Compatibility View, which will emulate IE7 and omit window.location.origin.
Opera Full support 10Safari Full support Yes
Notes
Full support Yes
Notes
Notes According to Apple's documentation, window.location.origin is supported since Safari 10 (both desktop and mobile), but the feature seems to be present in some older versions as well. Because of this, the exact versions supporting this feature cannot be determined reliably.
WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26
Notes
Full support 26
Notes
Notes Before Firefox 49, results for URL using the blob scheme incorrectly returned null.
Opera Android ? Safari iOS Full support 5Samsung Internet Android Full support Yes
passwordChrome Full support YesEdge Full support ≤79Firefox Full support 26IE ? Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
pathnameChrome Full support YesEdge Full support 12Firefox Full support 22
Notes
Full support 22
Notes
Notes Before 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".
IE Full support Yes
Notes
Full support Yes
Notes
Notes Internet Explorer does not provide the leading slash character in the pathname (docs/Web/API/Location instead of /docs/Web/API/Location).
Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22
Notes
Full support 22
Notes
Notes Before 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".
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
portChrome Full support YesEdge Full support 12Firefox Full support 22IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
protocolChrome Full support YesEdge Full support 12Firefox Full support 22IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
reloadChrome Full support 1Edge Full support 12
Notes
Full support 12
Notes
Notes In EdgeHTML versions of Edge, 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). This behavior was fixed in Edge 79.
Firefox Full support 1IE Full support 5.5
Notes
Full support 5.5
Notes
Notes 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).
Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
replaceChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
searchChrome Full support YesEdge Full support 12Firefox Full support 22
Notes
Full support 22
Notes
Notes Before 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".
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22
Notes
Full support 22
Notes
Notes Before 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".
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
toStringChrome Full support 52Edge Full support 12Firefox Full support 22IE Full support 11
Notes
Full support 11
Notes
Notes Intranet sites are set to Compatibility View, which will emulate IE7 and omit window.location.toString.
Opera ? Safari ? WebView Android Full support 52Chrome Android Full support 52Firefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android Full support 6.0
usernameChrome Full support YesEdge Full support ≤79Firefox Full support 26IE ? Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

See also