trying to prevent the back button from working on one of my Pages. JavaScript
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>开发者_JAVA技巧
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.
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
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.
精彩评论