7

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.

Anil Namde
  • 6,452
  • 11
  • 63
  • 100
  • 13
    The good (not necessarily common) practice is to make sure that your application actually works, even when faced with a user that uses his browser as it's designed. – Jakob Borg Feb 25 '10 at 17:30
  • 1
    This has to be one of the most-duplicated questions on SO. I'm surprised none of the five people answering it remembers seeing this question ten times or so. – John Saunders Feb 25 '10 at 19:32
  • @John: nope, I'm only a member for a month or so. I can see there are several related questions to the right... – Marcel Korpel Feb 25 '10 at 19:43
  • Thanks all for responding. Sorry for redundant question. – Anil Namde Feb 26 '10 at 14:04

5 Answers5

12

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
  • 43,168
  • 21
  • 122
  • 188
3

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
  • 57,086
  • 61
  • 201
  • 257
1

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: Change the get parameters in the address bar in jquery

Community
  • 1
  • 1
Mic
  • 24,812
  • 9
  • 57
  • 70
0

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
  • 377,238
  • 77
  • 533
  • 578
0

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
  • 21,536
  • 6
  • 60
  • 80