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>
document.write(location.href);
</script>
The output of the code above is:
http://www.w3schools.com/js/js_window_location.asp
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>
document.write(location.pathname);
</script>
The output of the code above is:
/js/js_window_location.asp
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>
<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 »
No comments:
Post a Comment