views:

88

answers:

3

I'm using anchors for dealing with unique urls for an ajaxy website. However, I want to reload the content when the user hits the Browser's "back" button so the contents always matches the url.

How can I achieve this? Is there a jQuery event triggering when user clicks "Back"?

+1  A: 

If you're using ajax urls and not reloading the page, I'm guessing you have #something style URLs, using hash navigation. If that's the case you probably want the window.onhashchange event to reload the appropriate content.

Since it's not supported by all browsers, there's a plugin for this, your code would look something like this:

$(window).hashchange(function() {
  //do something with location.hash that just changed
});
Nick Craver
+1  A: 

You need to use a hashchange event. There are a couple of popular plugins. I've been using the address plugin and like it. Some will suggest BBQ also.

Address plugin

jQuery BBQ

fehays
A: 

YUI history manager does this by checking the hash every 10ms using setInterval. There is no event registered by back, so that is probably the only way possible.

AutoSponge
The event exists in some browsers, it's best to use it where it exists and poll when it doesn't, the plugin in my answer does exactly this.
Nick Craver
Right on, learned something today:)
AutoSponge