X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Window Location


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.

Example

Display the href (URL) of the current page:

"Page location is " + window.location.href;

Result is:



Try it Yourself »


Window Location Hostname

The window.location.hostname property returns the name of the internet host (of the current page).

Example

Display the name of the host:

"Page host is " + window.location.hostname;

Result is:


Try it Yourself »


Window Location Pathname

The window.location.pathname property returns the path name of page.

Example

Display the path name of the current URL:

"Page path is " + window.location.pathname;

Result is:


Try it Yourself »


Window Location Protocol

The window.location.protocol property returns the web protocol of the page.

Example

Display the web protocol:

"Page protocol is " + window.location.protocol;

Result is:


Try it Yourself »


Window Location Assign

The window.location.assign() method loads a new document.

Example

Load a new document:

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

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

</body>
</html>

Try it Yourself »



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook