You can use the ‘location.hash’ property in JavaScript to get the hash value (the part of the URL after the “#” symbol) of the current page. For example:

console.log(location.hash);

This will output the current hash value in the browser’s console.

You can also use the ‘.substring()’ method to retrieve the hash value from the ‘window.location.href’ property:

console.log(window.location.href.substring(window.location.href.indexOf("#") + 1));

This will also output the current hash value in the browser’s console.

You can also use the ‘.split()’ method with the “#” separator to retrieve the last element of the array which will be the hash value.

console.log(window.location.href.split("#")[1]);

This will also output the current hash value in the browser’s console.

Also Read:

Categorized in: