Window Location Object
The location object in JavaScript provides information about the browser’s location, i.e., URLs. It is a built-in property of both the window and document objects. We can access it using either window.location or document.location.
The ‘location’ object contains various properties and methods to get and manipulate the information of the browser’s location (URL).
JavaScript Location Object Properties
We can use the properties of the location object to get information of URL:
- hash − This property is used to set or get the anchor part of the URL.
- host − This property is used to set or get the hostname or port number of the URL.
- hostname − This property is used to set the hostname.
- href − This property is used to set or get the URL of the current window.
- origin − This property returns the protocol, domain, and port of the URL.
- pathname − This property updates or gets the path name.
- port − This property updates or gets the port of the URL.
- protocol − This property updates or gets the protocol.
- search − This property is used to set or get the query string of the URL.
Syntax
Follow the syntax below to access the location object’s properties and methods −
window.location.property;
OR
location.property;
You may use the ‘window’ object to access the ‘location’ object.
Here, we have demonstrated the use of some properties of the location object with examples.
Example: Accessing location host property
The location.host property returns the host from the current URL. However, you can also change the host using it.
<html><body><div id="output"></div><script>const host = location.host;document.getElementById("output").innerHTML ="The host of the current location is: "+ host;</script></body></html></pre>
Output
The host of the current location is:Example: Accessing location protocol property
The location.protocol propery is used to get used in the current URL. You can also use it to update the protocol.
Try the following example to use the location.protocol property -
<html><body><div id ="output"></div><script>document.getElementById("output").innerHTML ="The protocol of the current location is: "+ location.protocol;</script></body></html></pre>
Output
The protocol of the current location is: https:Example: Accessing location hostname property
The location.hostname property returns the host name of the current URL. You can use it to the hostname as well.
Try the following example to use location.hostname property
<html><body><div id ="output"></div><script>document.getElementById("output").innerHTML ="The host name of the current location is: "+ location.hostname;</script></body></html></pre>
Output
The host name of the current location is: www.tutorialspoint.comExample: Accessing location pathname property
The location.pathname property returns the path name of the current location. You can set the path name using it.
<html><body><div id ="output"></div><script>document.getElementById("output").innerHTML ="The protocol of the current location is: "+ location.pathname;</script></body></html></pre>
Output
The protocol of the current location is: /javascript/javascript_location_object.htmJavaScript Location Object Methods
We can also use the methods of the location object to navigation to new URLs −
- assign(url) − This method loads a new document at the specified URL.
- replace(url) − This method replaces the current document with a new document at the specified URL.
- reload() − This method reloads the current document.
JavaScript location assign() method
The location.assign() method takes the URL and changes the URL in the current window. In short, it opens a new web page.
Syntax
Follow the syntax below to use the location.assign() method in JavaScript −
location.assign();
Example
In the below code, when you click the 'Go to home page' button, it will redirect you to the home page of the tutorialpoint website.
<html><body><div id="output"></div><button onclick="changePage()">Go to Home Page</button><script>let output = document.getElementById("output");functionchangePage(){window.location.assign("https://www.tutorialspoint.com/");}</script></body></html></pre>
Location Object Properties List
Here, we have listed all properties of the Location object.
Property Description hash It is used to set or get the anchor part of the URL. host It is used to set or get the hostname or port number of the URL. hostname It is used to set the hostname. href It is used to set or get the URL of the current window. origin It returns the protocol, domain, and port of the URL. pathname To update or get the path name. port To update or get the port of the URL. protocol To update or get the protocol. search To set or get the query string of the URL. Location Object Methods List
Here, we have listed all methods of the Location object.
Method Description assign() To load resources at a particular URL. reload() To reload the web page. replace() To replace the resources of the current webpage with another webpage's resources. toString() Returns the URL in the string format.
Leave a Reply