Search code examples
javascriptasp.net-mvc-2back-button

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>

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.


Solution

  • 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