0

I'm trying to prevent the back button from working on one of my asp.net mvc pages. I've read a couple of places that if i add "window.history.forward();" it will prevent the back button from working on a given page. This is what I did in my page:

<script type="text/javascript">        
$(document).ready(function () 
{             
    window.history.forward(); 
});     
</script>

I can't seem to get this to work. has anyone had any luck with this method? perhaps I'm taking the wrong approach. any help would be appreciated. thanks.

2
  • 4
    As a web user I can attest to the fact that its bugs the crap out of me when a site tries to mess with my browser navigation. Do you really need to do this? Commented Dec 31, 2010 at 16:23
  • I'm just messing around. I agree with you. Commented Dec 31, 2010 at 16:37

2 Answers 2

3

Don't break expected browser behaviour. You cannot effectively stop the BACK button from being used.

You're better off informing the user that pressing BACK will cause problems. You can hook an event to "beforeunload" to detect the user leaving the page.

See: Custom beforeunload prompt with javascript

Sign up to request clarification or add additional context in comments.

2 Comments

Furthermore, if there is a very real need why you want to stop this behavior (such as to stop duplicate orders) then one should be aware that JavaScript can be disabled, or fail to run (if there was a previous error), and so relying on JS completely to prevent something from happening is a sure fire way to have problems when it invariably happens anyway.
I'm just messing around with some stuff. nothing to do with orders.
3

This is not an answer to your question, but to your problem:

Your problem is probably not how to disable the back button, but rather why you'd want to do that. It's a horrible intrusion of the interface that users are used to. Think of what it is that you've done that "requires" this and how you could change that to work even when allowing the user to go back a page.

1 Comment

I agree. this is bad. I'm just doing some tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.