How to get the anchor from the URL using jQuery?

For current window, you can use this:

var hash = window.location.hash.substr(1);

To get the hash value of the main window, use this:

var hash = window.top.location.hash.substr(1);

If you have a string with an URL/hash, the easiest method is:

var url="https://www.stackoverflow.com/questions/123/abc#10076097";
var hash = url.split('#').pop();

If you’re using jQuery, use this:

var hash = $(location).attr('hash');

Leave a Comment