tags:

views:

209

answers:

5

Possible Duplicate:
Disabling Back button on the browser

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

+22  A: 

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
Rep bait, but true.
Sam152
@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
I'm not blaming you, I was about to write out the exact same answer.
Sam152
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
@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
+1  A: 

You cannot and you never should attempt to do so.

Pez Cuckow
+2  A: 

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

Polybos
+3  A: 

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

AOI Karasu
+1  A: 

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