WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Window Location


The window.location article can be utilized to get the present page address (URL) and to divert the program to another page.


Window Location

The window.location item can be composed without the window prefix.

Some examples:

  • window.location.href restores the href (URL) of the current page
  • window.location.hostname restores the space name of the web host
  • window.location.pathname restores the way and filename of the current page
  • window.location.protocol restores the web convention utilized (http://or https://)
  • window.location.assign loads another document

Window Location Href

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

Example

Display the href (URL) of the current page:

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

Result is:


Try it Yourself »

Window Location Hostname

The window.location.hostname property restores the name of the web have (of the current page).

Example

Display the name of the host:

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

Result is:

Try it Yourself »

Window Location Pathname

The window.location.pathname property restores the pathname of the current page.

Example

Display the way name of the current URL:

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

Result is:

Try it Yourself »

Window Location Protocol

The window.location.protocol property restores the web convention of the page.

Example

Display the web protocol:

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

Result is:

Try it Yourself »

Window Location Assign

The window.location.assign() strategy stacks another document.

Example

Load another document:

<html>
<head>
<script>
work newDoc() {
    window.location.assign("http://www.welookups.com")
}
</script>
</head>
<body>

<input type="button" value="Load new document" onclick="newDoc()">

</body>
</html>
Try it Yourself »