开发者

Hashchange jQuery plugin - obtaining the current hash when clicking an anchor

I'm using hashchange jQuery plugin from http://benalman.com/projects/jquery-hashchange-plugin/ to hook up an event when the window's location.hash changes.

I'd like to trigger a function when the hash changes that passes the new hash value (obtained with event.fragment) and the current hash value (the value just before the event is fired).

Here's a snipped of what I'd like to achieve:

$(window).bind('hashchange', function(event){
   myFunction(event.fragment, /* currentHash */);
});

Is开发者_高级运维 this possible?


There's a property on location:

$(window).bind('hashchange', function(event){
   myFunction(event.fragment, location.hash);
});

Or, store it yourself:

var lastHash = location.hash;                 //set it initially
$(window).bind('hashchange', function(event){
   myFunction(event.fragment, hashLash);      //previous hash
   lastHash = location.hash;                  //update it
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜