HOME HTML CSS JAVASCRIPT SQL PHP JQUERY 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:

  • location.hostname returns the domain name of the web host
  • location.pathname returns the path and filename of the current page
  • location.port returns the port of the web host (80 or 443)
  • location.protocol returns the web protocol used (http:// or https://)

Window Location Href

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

Example

Return the entire URL (of the current page):

<script>

document.write(location.href);

</script>

The output of the code above is:




Window Location Pathname

The location.pathname property returns the path name of a URL.

Example

Return the path name of the current URL:

<script>

document.write(location.pathname);

</script>

The output of the code above is:




Window Location Assign

The 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 BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE