The Window.location object is used to obtain the address (URL) of the current page and redirect the browser to a new page.
Window Location
The window.location object can be written without using the window prefix. Some examples:
Some examples:
- Location.hostname returns the domain name of the web host
- Location.pathname returns the path and file name of the current page
- Location.port returns the port of the web host (80 or 443)
- Location.protocol returns the Web protocol used (HTTP///https://)
Window location Href
The Location.href property returns the URL of the current page.
Instance
Returns the entire URL (of the current page):
<script>
document.write (LOCATION.HREF);
</script>
The above code output is:
Http://www.w3cschool.cc/js/js-window-location.html
Window location Pathname
The Location.pathname property returns the path name of the URL.
Instance
Returns the path name of the current URL:
<script>
document.write (Location.pathname);
</script>
The above code output is:
/js/js-window-location.html
Window location Assign
The Location.assign () method loads the new document.
Instance
To load a new document:
<script>
function Newdoc ()
{
Window.location.assign ("http://www.w3cschool.cc")
}
</script>
<body>
<input type= "button" value= "Load New Document" onclick= "Newdoc ()" >
</body>
JavaScript Window Location