Zak
Zak

Reputation: 2698

jQuery (or regular Javascript) execute function when page is loaded AFTER back button is pressed

I've seen this on a lot of websites, but I can't figure out exactly how to do it:

When a function is executed, the URL changes to whatever.com/page/#something and then when that url is accessed, the function is executed again. I know how to make that work when the page is first loaded, but I can't figure out how to make the function occur again when the "back" button is pressed.

(e.g. if I have a tabbed system set up, I start on site.com/#tab1, go to site.com/#tab2, and click the back button so that tab 1 shows up on the page again)

How does that work?

Upvotes: 2

Views: 519

Answers (1)

justkt
justkt

Reputation: 14766

You'll want to look into history management in Javascript. If you aren't using the browser's built-in functionality to maintain history - which is how the back and forward buttons work - you'll need to do a bit of work yourself.

jQuery has several plugin options to help you out. hashchange for example, looks to provide the event you're looking for with an onhashchange event added for browsers that don't have it implemented yet.

Upvotes: 2

Related Questions