JavaScript Window Location

In this tutorial you will learn about the JavaScript window location object.

The Location Object

The location property of a window (i.e. window.location) is a reference to a Location object; it represents the current URL of the document being displayed in that window.

Since window object is at the top of the scope chain, so properties of the window.location object can be accessed without window. prefix, for example window.location.href can be written as location.href. The following section will show you how to get the URL of page as well as hostname, protocol, etc. using the location object property of the window object.

Getting the Current Page URL

You can use the window.location.href property to get the entire URL of the current page.

The following example will display the complete URL of the page on button click:

function getURL() {
    alert("The URL of this page is: " + window.location.href);
<button type="button" onclick="getURL();">Get Page URL</button>

Getting Different Part of a URL

Similarly, you can use other properties of the location object such as protocol, hostname, port, pathname, search, etc. to obtain different part of the URL.

Try out the following example to see how to use the location property of a window.

// Prints complete URL
// Prints protocol like http: or https:
// Prints hostname with port like localhost or localhost:3000
// Prints hostname like localhost or
// Prints port number like 3000
// Prints pathname like /products/search.php
// Prints query string like ?q=ipad
// Prints fragment identifier like #featured

Note: When you visit a website, you're always connecting to a specific port (e.g. http://localhost:3000). However, most browsers will simply not display the default port numbers, for example, 80 for HTTP and 443 for HTTPS.

Loading New Documents

You can use the assign() method of the location object i.e. window.location.assign() to load another resource from a URL provided as parameter, for example:

function loadHomePage() {
<button type="button" onclick="loadHomePage();">Load Home Page</button>

You can also use the replace() method to load new document which is almost the same as assign(). The difference is that it doesn't create an entry in the browser's history, meaning the user won't be able to use the back button to navigate to it. Here's an example:

function loadHomePage(){
<button type="button" onclick="loadHomePage();">Load Home Page</button>

Alternatively, you can use the window.location.href property to load new document in the window. It produce the same effect as using assign() method. Here's is an example:

function loadHomePage() {
    window.location.href = "";
<button type="button" onclick="loadHomePage();">Load Home Page</button>

Reloading the Page Dynamically

The reload() method can be used to reload the current page dynamically.

You can optionally specify a Boolean parameter true or false. If the parameter is true, the method will force the browser to reload the page from the server. If it is false or not specified, the browser may reload the page from its cache. Here's an example:

function forceReload() {
<button type="button" onclick="forceReload();">Reload Page</button>

Note: The result of calling reload() method is different from clicking browser's Reload/Refresh button. The reload() method clears form control values that otherwise might be retained after clicking the Reload/Refresh button in some browsers.

Your Feedback:

We would love to hear from you, please drop us a line.