English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The location property of the window (i.e., window.location) is a reference to the Location object.
window.locationThe object represents the current URL of the document displayed in the window.
It can be written without using the window prefix window.locationObject.
Some examples:
The next section will show you how to use the location object properties to get the page URL, hostname, protocol, and so on.
Thelocation.hrefThe property returns the URL of the current page.
var x = location.href;Test See‹/›
Thelocation.hostnameThe property returns the name of the Internet host (current page).
var x = location.hostname;Test See‹/›
Thelocation.pathnameThe property returns the pathname of the current page.
The pathname is a string that starts from the hostname and includes an initial /followed by the path of the URL.
var x = location.pathname;Test See‹/›
Thelocation.protocolThe property returns the web protocol of the current URL, including the colon(:).
var x = location.protocol;Test See‹/›
Thelocation.portThe property returns the Internet host port number of (the current page).
var x = location.port;Test See‹/›
Note:If the port number is not specified in the URL or it is the default port of the scheme (http's80 and https443),then the port attribute returns an empty string.
location.assign()The method makes the window load and display the document with the specified URL.
location.assign("https://www.oldtoolbag.com");Test See‹/›
For a complete reference to properties and methods, please visit ourJavaScript Location Object Reference.
The reference section includes descriptions and examples of all location properties and methods.