This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.
The
property returns the URL of the current pageDisplay the href (URL) of the current page:
document.getElementById("demo").innerHTML = "Page location is " + window.location.href;
Result is:
Page location is
The
property returns the name of the internet host (of the current page).Display the name of the host:
document.getElementById("demo").innerHTML = "Page hostname is " + window.location.hostname;
Result is:
Page hostname is
The
property returns the pathname of the current page.Display the path name of the current URL:
document.getElementById("demo").innerHTML = "Page path is " + window.location.pathname;
Result is:
Page path is
The
property returns the web protocol of the page.Display the web protocol:
document.getElementById("demo").innerHTML = "Page protocol is " + window.location.protocol;
Result is:
Page protocol is
The
property returns the number of the internet host port (of the current page).Display the port number:
document.getElementById("demo").innerHTML = "Port number is " + window.location.port;
Result is:
Port number is
Most browsers will not display default port numbers (80 for http and 443 for https)
The
method loads a new document.Load a new document:
<html> <head> <script> function newDoc() { window.location.assign("https://www.w3schools.com") } </script> </head> <body> <input type="button" value="Load new document" onclick="newDoc()"> </body> </html>
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon