Greg
Greg

Reputation: 21909

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?

Upvotes: 0

Views: 1281

Answers (1)

Nick Craver
Nick Craver

Reputation: 630429

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

Upvotes: 2

Related Questions