Before jumping right to the properties of the Location Object, let’s first understand what a location object is.
Location Object
The location object consists of the relevant information about the available URL and like document object, history object, and screen object, it is also a property of the window object.
Syntax
The syntax of the location object is as follows.
Or,
Example
In the following example, we are using the pathname property of the location object to fetch the pathname of the web page.
Output
Using the pathname property of the location object we have fetched the pathname of the existing URL.
Now that we have a basic understanding of the location object, let’s dive into the details of the properties of the location object.
Properties of the Location Object
Location object properties are as follows.
1. hash
It is used for the purpose of fetching or setting the anchor of the URL (including the hash#).
Syntax
The syntax of the hash property is given below.
For fetching,
For setting,
Example
Suppose you want to get the anchor part of a url using the hash property of the location object.
<html>
<body>
<p><a id="linux" href="https://linuxhint.com/linux-command-cheat-sheet/#linux-command-cheat-sheet">Linux Command Cheat Sheet</a><p>
<p id="tutorial"></p>
<script>
let url = document.getElementById("linux");
document.getElementById("tutorial").innerHTML = "The anchor portion of the URL is: " + url.hash;
</script>
</body>
</html>
In the above example, we provided a link to the href attribute of the <a> element then we used the hash property over the link to get the anchor portion of the URL.
Output
Using the hash property of the location object we extracted the anchor portion of the URL.
2. host
It is used for the purpose of extracting the hostname and port number of the URL.
Syntax
The host property syntax is given below.
For fetching the host of the URL,
For setting the host of the URL,
Example
Suppose you want to fetch the hostname of the existing URL using the host property of the location object.
In the above example, we are getting the hostname of the available URL using the following piece of code.
document.getElementById("tutorial").innerHTML = host;
Output
Using the host property of the location object the hostname and port number of the existing URL has been fetched.
3. hostname
It is used for fetching the hostname of the URL.
Syntax
The hostname property syntax is provided below.
For fetching the hostname of the URL,
For setting the hostname of the URL,
Example
Suppose, you want to extract the hostname of the at hand URL.
In the above example, the hostname property of the location object was used to get the hostname of the available URL.
document.getElementById("tutorial").innerHTML = hostname;
Output
Using the hostname property of the location object the hostname of the present URL has been fetched.
4. href
It is used for the purpose of fetching or setting the complete URL.
Syntax
The href property syntax is given below.
For extracting the href of the URL,
For setting the href of the URL,
Example
Suppose you want to extract the complete URL of existing web page.
In the above example, using the href property of the location object, the complete URL of the at hand web page is being fetched.
document.getElementById("tutorial").innerHTML = url;
Output
The complete URL of the present web page has been extracted using the href property of the location object.
5. origin
It is used for the purpose of fetching the hostname, port number, and protocol of the URL.
Syntax
The syntax of the origin property is as follows.
Example
Suppose you want to fetch the protocol, hostname and port number of the present URL.
In the above example, the origin (protocol, hostname, and port number) of the present URL is being fetch using origin property of the location object.
document.getElementById("tutorial").innerHTML = origin;
Output
The output displays the protocol, hostname, and port number of the available URL.
6. pathname
It is used for the purpose of extracting or setting the pathname of the URL.
Syntax
The pathname property syntax is as follows.
For fetching the pathname of the URL,
For setting the pathname of the URL,
Example
Suppose you want to extract the pathname of the present URL.
In the above example, the pathname of the existing URL is being extracted using the pathname property of the location object.
document.getElementById("tutorial").innerHTML = path;
Output
Using the pathname property of the location object, the pathname of the existing URL has been fetched.
7. port
It is used for the purpose of extracting or setting the port number of the URL.
Syntax
The port property syntax is as follows.
For fetching the port of the URL,
For setting the port of the URL,
Example
In the following example, the port number of the present web page is being extracted.
In the above example, using the port property of the location object, the port number of the present web page is being extracted.
document.getElementById("tutorial").innerHTML = "The port number of the current web page is: " + port;
Output
The port number of the present web page has been fetched and shown in the output.
8. protocol
It is used for the purpose of fetching or setting the protocol of the URL.
Syntax
The protocol property syntax is as follows.
For extracting the port of the URL,
For setting the port of the URL,
Example
In the following example, the protocol of the available URL is being fetched.
The following piece of code fetches the protocol of the present URL.
document.getElementById("tutorial").innerHTML = protocol;
Output
The protocol of the present URL has been extracted.
9. search
It is used for the purpose of fetching or setting the querstring of the URL.
Syntax
The search property syntax is as follows.
For extracting the search of the URL,
For setting the search of the URL,
Example
<html>
<body>
<p><a id="linux" href="https://linuxhint.com/linux-command-cheat-sheet/?answer=yes">
https://linuxhint.com/linux-command-cheat-sheet/?answer=yes</a></p>
<p id="tutorial"></p>
<script>
let anchor = document.getElementById("linux");
let query = anchor.search;
document.getElementById("tutorial").innerHTML = "The search portion of the URL is: " + query;
</script>
</body>
</html>
The following piece of code fetches the querystring of the URL.
let query = anchor.search;
document.getElementById("tutorial").innerHTML = "The search portion of the URL is: " + query;
Output
The query string of the URL has been extracted.
Conclusion
The location object consists of the relevant information about the available URL and like document object, it is also a property of the window object. It consists of many properties such as hash, host, hostname, pathname, etc. These properties have different purposes that are highlighted in this post along with suitable examples.