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.
The Location
interface doesn't inherit any property, but implements those from URLUtils
.
Location.href
DOMString
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
DOMString
containing the protocol scheme of the URL, including the final ':'
.Location.host
DOMString
containing the host, that is the hostname, a ':'
, and the port of the URL.Location.hostname
DOMString
containing the domain of the URL.Location.port
DOMString
containing the port number of the URL.Location.pathname
DOMString
containing an initial '/'
followed by the path of the URL.Location.search
DOMString
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
DOMString
containing a '#'
followed by the fragment identifier of the URL.Location.username
DOMString
containing the username specified before the domain name.Location.password
DOMString
containing the password specified before the domain name.Location.origin
Read only
DOMString
containing the canonical form of the origin of the specific location.The Location
interface doesn't inherit any method, but implements those from URLUtils
.
Location.assign()
Location.reload()
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()
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()
DOMString
containing the whole URL. It is a synonym for URLUtils.href
, though it can't be used to modify the value.// 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
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. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
assign |
Yes | Yes | Yes | Yes | Yes | Yes |
hash |
Yes | Yes | 22 | Yes | Yes | Yes |
host |
Yes | Yes | 22 | Yes | Yes | Yes |
hostname |
Yes | Yes | 22 | Yes | Yes | Yes |
href |
Yes | Yes | 22 | Yes | Yes | Yes |
origin |
Yes | Yes | 26
|
11
|
? | ? |
password |
Yes | ? | 26 | ? | ? | ? |
pathname |
Yes | Yes | 22
|
Yes | Yes | Yes |
port |
Yes | Yes | 22 | Yes | Yes | Yes |
protocol |
Yes | Yes | 22 | Yes | Yes | Yes |
reload |
Yes | Yes | Yes | Yes | Yes | Yes |
replace |
Yes | Yes | Yes | Yes | Yes | Yes |
search |
Yes | Yes | 22
|
Yes | Yes | Yes |
toString |
52 | Yes | 22 | 11
|
? | ? |
username |
Yes | ? | 26 | ? | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | ? | Yes | Yes | Yes | Yes | Yes |
assign |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
hash |
Yes | Yes | Yes | 22 | Yes | Yes | Yes |
host |
Yes | Yes | Yes | 22 | Yes | Yes | Yes |
hostname |
Yes | Yes | Yes | 22 | Yes | Yes | Yes |
href |
Yes | Yes | Yes | 22 | Yes | Yes | Yes |
origin |
Yes | Yes | Yes | 26
|
? | ? | ? |
password |
Yes | Yes | ? | 26 | ? | ? | ? |
pathname |
Yes | Yes | Yes | 22
|
Yes | Yes | Yes |
port |
Yes | Yes | Yes | 22 | Yes | Yes | Yes |
protocol |
Yes | Yes | Yes | 22 | Yes | Yes | Yes |
reload |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
replace |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
search |
Yes | Yes | Yes | 22
|
Yes | Yes | Yes |
toString |
52 | 52 | Yes | 22 | ? | ? | ? |
username |
Yes | Yes | ? | 26 | ? | ? | ? |
Window.location
and Document.location
.URL
, URLSearchParams
and HTMLHyperlinkElementUtils
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/location