English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Object

JavaScript Function

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JS Window Location

The location property of the window (i.e., window.location) is a reference to the Location object.

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.

Get the URL of the current page

Thelocation.hrefThe property returns the URL of the current page.

var x = location.href;
Test See‹/›

Get the hostname

Thelocation.hostnameThe property returns the name of the Internet host (current page).

var x = location.hostname;
Test See‹/›

Get the current page path name

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‹/›

Get Current Page Protocol

Thelocation.protocolThe property returns the web protocol of the current URL, including the colon(:).

var x = location.protocol;
Test See‹/›

Get Port Number

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.

Window Location Assignment

location.assign()The method makes the window load and display the document with the specified URL.

location.assign("https://www.oldtoolbag.com");
Test See‹/›

Complete Location Reference

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.