views:

134

answers:

5

Is there any way using JavaScript(or something else) to detect/prevent that user clicked on browser back/forward button ?

I would like to know about the common practice to deal with this issue. This is really pain especially in the work flow like ordering.

A: 

Nope this is not possible, you can not usurp the browsing experience from a user, but hope there should be a work around to this....

Sarfraz
A: 

As far as I know, not, but you can use the window.onbeforeunload event to warn users not to go away, when risking data loss. Please bear in mind this doesn't work on Opera.

Marcel Korpel
+6  A: 

Actually preventing the user from using the back or forward button is bad usability and will frustrate users a lot - to be blunt, it's passing a developer problem back to the user.

I think the Post/Redirect/Get pattern, coupled with window.onbeforeunload in javascript (like SO uses) would be really useful for your circumstances.

Gavin Miller
It's actually `window.onbeforeunload` (I just made this typo myself).
Marcel Korpel
@Marcel - Thanks for pointing that out.
Gavin Miller
+2  A: 

look into a history manager. something like this:

http://code.google.com/p/reallysimplehistory/

you can make the back button actually do something useful, like going back to previous (valid) state, if possible.

mkoryak
+1  A: 

If you build a one page web app (meaning a single page is loaded, then all changes are done dynamically in that page with Javascript) you have options:

When the user clicks the back button, you leave the page, since it's the only one, and you can warn the user through the event window.onbeforeunload

And then, if you need/want more control of the back/next button behaviour, you can add a hash( # ) key to handle that.
See my answer to that question: http://stackoverflow.com/questions/2258719

Mic