In this article I will explain with an example, how to prevent user from navigate to previous page using back button of the browser or the back option in the context menu in ASP.Net.
One cannot disable the Browser Back Button functionality only thing that can be done is prevent them.
Two pages are used i.e. Login and Home. After Login, User is sent to Home page and using Browser Back Button he will be prevented from going back to Login page from Home page.
 
 
Disable Browser Back Button JavaScript function
The following function must be placed inside the Page where you want to prevent the User from coming back using Browser Back Button.
For example, consider two pages, Login and Home and once User lands on the Home page you would not want him to go to Login page. Thus, the following script must be placed inside the Login page.
Inside the preventBack function, the window.history.forward() method is called which navigates the browser forward one step in their history. Whenever Browser Back Button is clicked, this function immediately moves it one step forward.
The setTimeout method schedules the execution time of the preventBack function with delay of 0 (zero) milliseconds.
Finally, the window.onload function returns null.
<script type="text/javascript">
    function preventBack() {
        window.history.forward();
    }
    setTimeout("preventBack()", 0);
    window.onunload = function () {
        null
    };
</script>
 
 
Disable Browser Back Button implementation
Login Page
The HTML Markup of Login page consists of an HTML Anchor tag.
The href property of the Anchor tag is set to name of Home page.
The script to disable the Browser Back Button is placed inside the HEAD section.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Login</title>
    <script type="text/javascript">
        function preventBack() {
            window.history.forward();
        }
        setTimeout("preventBack()", 0);
        window.onunload = function () {
            null
        };
    </script>
</head>
<body>
    <h3>Login</h3>
    <hr />
    <a href = "Home.htm">Redirect to Home</a>
</body>
</html>
 
Home Page
The HTML Markup of Home page consists of an HTML Anchor tag.
The href property of the Anchor tag is set to name of Login page.
Note: HTML Anchor link is added to illustrate that the Login page will be prevented only using Browser Back Button and not when accessed using other means.
 
Disabling Browser Back Button Script
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Home</title>
</head>
<body>
    <h3>Home</h3>
    <hr />
    <a href="Login.htm">Redirect to Login</a>
</body>
</html>
 
 
Screenshot
Disable Browser Back Button Functionality using JavaScript in ASP.Net
 
 
Browser Compatibility
The above code has been tested in the following browsers only in versions that support HTML5.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 
Demo
 
 
Downloads


Other available versions