-2

Possible Duplicate:
Disabling Back button on the browser

How do I disable the back button of a browser using JavaScript?

Community
  • 1
  • 1
arun
  • 9
  • 1

5 Answers5

24

You can't, and you shouldn't try to. The users browser is expected to behave a certain way, you should not break that expectation. Your site should behave like the other 99.999999% of the internet, it shouldn't suddenly change the meaning of the users toolbar buttons, their very fundamental browser operation.

What are you trying to prevent? There's probably a much better solution to the problem, e.g. page cache timeout, etc. I would say form posting and not wanting to re-post is the most common problem you're trying to avoid, if that's the case, give another model a look.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 4
    @Sam152 - Unfortunately, not the first time I've seen this come up, really should be a FAQ of "things you should never *think* about doing as a web developer" somewhere. I try and give the OP alternatives to solving whatever problem they *actually* have...but I'm not sure what else to do to get the idea out of their head :-/ – Nick Craver Jun 12 '10 at 11:19
  • I'm not blaming you, I was about to write out the exact same answer. – Sam Becker Jun 12 '10 at 11:23
  • Maybe it's a site which loads part of its contents via ajax. Therefore clicking on the back button does not result in the expected behavoir. – Dave O. Jun 12 '10 at 12:13
  • @Dave - That may very well be the case, this is what I'm talking about in my answer, there's usually another way to handle this, e.g, `#step1`, `#step2` hash navigation, among others...but disabling the back button shouldn't be the solution. – Nick Craver Jun 12 '10 at 12:16
2

You shouldn't. But what's about opening a new browser window?

Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
2

You cannot manipulate browser's GUI from a web page on such detailed level. Consider the consequences - what a mess and abuse.

Aoi Karasu
  • 3,730
  • 3
  • 37
  • 61
1

You cannot and you never should attempt to do so.

Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
1

Users are free to re-open a page they have visited before, the back button helps them go back easily to the last page visited.

If this is for some "defensive" requirement, it is better to have it on the application level and not the browser.

bakkal
  • 54,350
  • 12
  • 131
  • 107