views:

43

answers:

1

Is it possible to catch the JavaScript event when a user hits the back button in the browser or if history.back() is triggered? Best solution would be with mootools.

I know there is window.onbeforeunload but this is triggered also when a form is submitted or a link is clicked...

+2  A: 

Unfortunately there isn't really any way of doing this. If you could, then the spam sites would have a field day.

You could potentially use window.onbeforeunload and have a global boolean variable on the page that you set to false if you don't want to trigger the event. For example, before submitting the form, set it to false, or when a link is clicked on the page, set it to false. Then just check against the variable.

EDIT: Sorry, just to be clear, I'm not sure if window.onbeforeunload even fires when the back button is clicked. I'm just talking hypothetically if it does.

Liggi
It does fire when the back button gets hit. At least in Chrome.
Ronald
That's your solution then. I'd check it in other browsers though.
Liggi