In this article I will explain with an example, how to disable Browser Back Button after login in ASP.Net using JavaScript.
One cannot disable the Browser Back Button functionality only thing that can be done is prevent them.
 
 

Concept

In this article, two Pages are used i.e. Login and Home. After Login, User is sent to Home page and he will be prevented from going back to Login page from Home page using Browser Back Button.
 
 

Disable Browser Back Button using 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.onunload 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 and its href property is set to name of Home page.
Finally, 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 Home page consists of an HTML Anchor tag and its href property of the Anchor link 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.
 
<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>
 
 

Screenshots

Disable Browser Back Button after login in ASP.Net using JavaScript
 
 

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