Full width home advertisement

How To

Tech

JavaScript

Post Page Advertisement [Top]

JavaScript Window Location

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.href returns the href (URL) of the current page
  • location.hostname returns the domain name of the web host
  • location.pathname returns the path and filename of the current page
  • location.protocol returns the web protocol used (http:// or https://)
  • location.assign loads a new document

Window Location Href

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

Example

Display the href (URL) of the current page:
"Page location is " + location.href;
Result is:
Page location is http://pkpioneer.blogspot.com/


Window Location Hostname

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

Example

Display the name of the host:
"Page host is " + location.hostname;
Result is:
Page hostname is www.pkpioneer.blogspot..com


Window Location Pathname

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

Example

Display the path name of the current URL:
"Page path is " + location.pathname;
Result is:
/js/js_window_location.asp


Window Location Protocol

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

Example

Display the web protocol:
"Page protocol is " + location.protocol;
Result is:
Page protocol is http:


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.pkpioneer.blogspot..com")
}
</script>
</head>
<body>

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

</body>
</html>

Bottom Ad [Post Page]