I have some jQuery/JavaScript code that I want to run only when there is a hash (#
) anchor link in a URL. How can you check for this character using JavaScript?
Usually clicks go first than location changes, so after a click is a good idea to setTimeOut to get updated window.location.hash
$(".nav").click(function(){
setTimeout(function(){
updatedHash = location.hash
},100);
});
or you can listen location with:
window.onhashchange = function(evt){
updatedHash = "#" + evt.newURL.split("#")[1]
};
I wrote a jQuery plugin that does something like what you want to do.
It's a simple anchor router.