简体   繁体   中英

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM