I need to prevent the user from being able to navigate back to the previous page in their browser.
To achieve this, I have included the following code snippet in the layout pages of my ASP.NET MVC application (in addition to setting appropriate response headers from the server):
function noBack() {
window.history.forward();
}
$(document).ready(function () {
$(document).on("pageshow", "body", function (event) {
if (event.persisted)
noBack();
});
});
...
<body onload = "noBack();">
I am facing two problems that I'm seeking solutions for:
1) Currently, this code is displaying a strange behavior consistently across all browsers. When I press the Back button on any browser, including IE, it briefly goes back to the previous page, loads it and displays it, then automatically forwards back to the original page. This is quite frustrating. How can I fix this issue?
2) The pageshow
event is not supported by IE, as far as I am aware. Is there an alternative event that I can use for IE? I need to ensure compatibility with IE 8 and newer versions, along with FireFox and Chrome.
Side note: I understand that modifying the functionality of the client's browser is generally not recommended, and it is usually preferable to disable output caching by configuring the server to send appropriate response headers. However, my client specifically requested that I also prevent the user from using the browser's back and forward buttons.