Window Location is a URL object. It is mostly used to redirect or reload the pages.
Properties
Property
Description
location.href
Full URL string.
location.protocol
Protocol Used in URL
location.host
Hostname string
location.hostname
Domain part of the URL
location.port
Port Number of the URL
location.pathname
String starting from / giving path from the URL
location.search
QueryString starting from ? from the URL
location.hash
String followed by # in URL
Above demo will give value of all properties of a simple URL ( without any query string or hash etc ) . We can create one location object by adding all the above properties to the URL and check the output.
<input type=button value='JavaScirpt Home by assign' onClick=window.location.assign('site_map.php');>
location.reload()
Reloads Current URL.
<input type=button value='Refresh this Page' onClick=window.location.reload();>
location.replace()
Replace with provided URL and opens the page.
While moving to provided ( URL) page , the current page will not be saved in session history, so you can't use Back button of browser to return to the parent ( this ) page. So a link is provided in Demo page to return to this page.
This is the main difference between location.assign() and location.replace()
<input type=button value='Demo page by Replace' onClick=window.location.replace('location-demo.php');>
location.toString()
Returns the full URL as string .
On Click location.toString() Method will return one alert Box with the URL String
<input type=button value='Demo toStringe() by Alert' onClick=alert(window.location.toString());>