Location
The read-only location property of the Document interface returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL.
To retrieve just the URL as a string, the read-only document.URL property can also be used.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 4 | 18 | 3.2 | |
| 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. | 1 | 12 | 1 | 1 | 18 | 1 |
| The ancestorOrigins read-only property of the Location interface is a static DOMStringList containing, in reverse order, the origins of all ancestor browsing contexts of the document associated with the given Location object. | 20 | 79 | 148 | 6 | 25 | 6 |
| The assign() method of the Location interface causes the window to load and display the document at the URL specified. After the navigation occurs, the user can navigate back to the page that called Location.assign() by pressing the "back" button. | 1 | 12 | 1 | 3 | 18 | 1 |
| The hash property of the Location interface is a string containing a "#" followed by the fragment identifier of the location URL. If the URL does not have a fragment identifier, this property contains an empty string, "". | 1 | 12 | 1 | 1 | 18 | 1 |
| The host property of the Location interface is a string containing the host, which is the Location.hostname, and then, if the port of the URL is nonempty, a ":", followed by the Location.port of the URL. If the URL does not have a hostname, this property contains an empty string, "". | 1 | 12 | 1 | 1 | 18 | 1 |
| The hostname property of the Location interface is a string containing either the domain name or IP address of the location URL. If the URL does not have a hostname, this property contains an empty string, "". IPv4 and IPv6 addresses are normalized, such as stripping leading zeros, and domain names are converted to IDN. | 1 | 12 | 1 | 1 | 18 | 1 |
| The href property of the Location interface is a stringifier that returns a string containing the whole URL, and allows the href to be updated. | 1 | 12 | 1 | 1 | 18 | 1 |
| The origin read-only property of the Location interface returns a string containing the Unicode serialization of the origin of the location's URL. | 8 | 12 | 21 | 5.1 | 18 | 5 |
| The pathname property of the Location interface is a string containing the path of the URL for the location. If there is no path, pathname will be empty: otherwise, pathname contains an initial '/' followed by the path of the URL, not including the query string or fragment. | 1 | 12 | 1 | 1 | 18 | 1 |
| The port property of the Location interface is a string containing the port number of the location's URL. If the port is the default for the protocol (80 for ws: and http:, 443 for wss: and https:, and 21 for ftp:), this property contains an empty string, "". | 1 | 12 | 1 | 1 | 18 | 1 |
| The protocol property of the Location interface is a string containing the protocol or scheme of the location's URL, including the final ":". | 1 | 12 | 1 | 1 | 18 | 1 |
| The reload() method of the Location interface reloads the current URL, like the Refresh button. | 1 | 12 | 1 | 1 | 18 | 1 |
| The replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the Location.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. Not to be confused with the String method… | 1 | 12 | 1 | 1 | 18 | 1 |
| The search property of the Location interface is a search string, also called a query string, that is a string containing a "?" followed by the parameters of the location's URL. If the URL does not have a search query, this property contains an empty string, "". | 1 | 12 | 1 | 1 | 18 | 1 |
| The toString() stringifier method of the Location interface returns a string containing the whole URL. It is a read-only version of Location.href. | 52 | 12 | 22 | 1 | 52 | 1 |
| The read-only location property of the Window interface returns a Location object with information about the current location of the document. | 1 | 12 | 1 | 1 | 18 | 1 |
- This browser only partially implements this feature
- This feature was removed in a later browser version (4)
- Only supported for `HTMLDocument`, not all `Document` objects.
- This browser only partially implements this feature
- This feature was removed in a later browser version (3.2)
- Only supported for `HTMLDocument`, not all `Document` objects.
- Before Firefox 49, results for URL using the blob scheme incorrectly returned null.
- Before Firefox 53, the `pathname` 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".
- Before Edge 79, 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).
- Before Firefox 53, the `search` 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".
- Before Firefox 57, single quotes contained in URLs were escaped when accessed via URL APIs. See bug 1386683.
Syntax
console.log(document.location);
// Prints a Location object to the console Use cases
-
Using Location
The read-only location property of the Document interface returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.