Erik Escobedo
Erik Escobedo

Reputation: 2803

How do I know if the user clicks the "back" button?

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"?

Upvotes: 2

Views: 1286

Answers (3)

AutoSponge
AutoSponge

Reputation: 1454

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.

Upvotes: 0

fehays
fehays

Reputation: 3167

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

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630607

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

Upvotes: 1

Related Questions