The window.location.hash
returns a string that contains a #
along with the fragment identifier of the URL. The fragment identifier of the URL starts with a #
followed by an identifier that uniquely identifies a section in an HTML document.
In this Answer, we will understand the working of the location.hash
property of the window object. The following are the two main uses of this property:
<a>
.The syntax that follows from the two uses is the one below:
//This is used to return the anchor part of the URL.
location.hash
//This is used to set the anchor value to some `ID` that we want the user to route to.
location.hash = anchorname
Note: The
location
in the syntax above refers to the actual location of the element in the HTML document. Thislocation
can be accessed using the methods provided to us byDOM
.
anchorname
: This is a string that specifies the anchor
of an URL.
The window.location.hash
property returns a string
which is the anchor
of the URL.
The code example below illustrates the first use of the property:
The explanation for the code that is present in the HTML file of the code widget above is as follows:
<a>
tag with an href
attribute encapsulates the <h2>
tag. This href
attribute is what we want to retrieve using the location.hash
property.<p>
tag that acts as a container that will dynamically render the fragment identifier of the URL when the user clicks on the <button>
present on line 8.<button>
tag listens for the onclick
event and executes the function in the Javascript tab in the code example above. The description of the code in the JavaScript tab is as follows:
document.getElementById()
to fetch respective HTML elements.<p>
tag in the HTML document using the element innerText
location.hash
property to assign the hash string
returned by the property and render it accordingly in the <p>
tag.Note: To read more on the
innerText
property, please refer to this link.
Let's look at the second use of the location.hash
property and look at its working with the help of a code example:
Note: We must not include the hash (
#
) sign to set theanchor
of the URL using this property. The code example that follows will further clarify this concept.
The explanation for the HTML code is as follows:
<button>
tag linked to the changeAnchor()
method defined in the JavaScript tab. This method is explained later in the Answer.<a>
tag with the href
attribute manipulated using the location.hash
property. It is first set to the value currenthash
and is changed to newhash
when the onclick
event is triggered.<div>
elements that are incorporated to demonstrate the second primary use of location.hash
property.The code in the JavaScript file has the changeAnchor()
function which encapsulates the code described below:
<button>
element using DOM
and change its text dynamically using the innerText
property when the user clicks on the <button>
present on line 5 of the HTML document. location.hash
property to set the href
attribute of the <a>
tag, as specified on line 6 of the HTML file.Free Resources