Jquery get hash value
Let’s say you have a link like this
<a href="http://ibnuyahya.com/list#123">my link </a>
and you want to get value after hash, you can use this.hash to get hash value in a link for example
$('a').click(function(){
alert( this.hash ); //return #123
});if you don’t want to include hash # in your result, just add slice(1)
$('a').click(function(){
alert( this.hash.slice(1) ); //return 123
});
Get Hash value from jQuery « blog.calyph.com 7:10 am on June 20, 2011 Permalink |
[...] tutorial ibnuyahya ← Creativity + Organization = [...]
Gibbs 12:37 am on January 31, 2012 Permalink |
Cheers for this Ibnuyahya. The most elegant solution I’ve come across after a search.
Brett Alton 11:28 pm on March 23, 2012 Permalink |
How do you retrieve it after the page has already loaded? e.g. not on click but after page load?
ibnuyahya 12:44 am on March 24, 2012 Permalink |
you can use document ready and windows.location to get current url.
$(document).ready(function() {
alert(window.location.hash.slice(1));
});