EN
JavaScript - get hash part from current URL
0 points
In this article, we would like to show you how to get the hash part from the current URL in JavaScript.
Quick solution:
xxxxxxxxxx
1
location.hash.substr(1);
or:
xxxxxxxxxx
1
var url = location.href; // Example URL: 'https://mail.google.com/mail/u/0/#inbox'
2
var index = url.indexOf('#');
3
4
if (index !== -1) {
5
var hashPart = url.substring(index + 1);
6
console.log(hashPart); // Output: 'inbox'
7
}
In this example, we get the hash part from the current url using substring()
method on location.hash
property.

In this example, we get the current URL using location.href
property. Then we get the substring from the URL starting from the #
character index.
