top of page

The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

Window Location

The window.location object can be written without the window prefix.

Some examples:

  • window.location.href returns the href (URL) of the current page

  • window.location.hostname returns the domain name of the web host

  • window.location.pathname returns the path and filename of the current page

  • window.location.protocol returns the web protocol used (http:// or https://)

  • window.location.assign loads a new document

Window Location Href

The window.location.href property returns the URL of the current page.

 

Display the href (URL) of the current page:

document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;

Result is:

thw loction of your page

Javascript Location Object

Action to Be  Performed

bottom of page