How to get hashtag value on hash change with example

How to get hashtag value on hash change with example

You can get the value of #hashtag from the URL if user click on any link in your page. There are two ways to find the id,  First by using Javascript and the second is by jQuery.

Javascript:

By using onhashchange event we can get the hash value, let's see the example:

window.onhashchange = callMyFunction;
function callMyFunction() {
    var hash = window.location.hash;
    alert(hash);
}

jQuery:

In the jQuery we can get the hash value by hashchange event, let's see the example:

$(window).on('hashchange', function (e) {
    alert(location.hash);
});

Related posts

Write a comment