In this article I will explain with an example, how to Disable Browser Back Button using JavaScript (jQuery) in ASP.Net MVC Razor.
The technique is to prevent user from navigate to previous page using Back Button of the browser or the back option in the Context Menu.
 
 
Disable Browser Back Button Script
The following JavaScript code snippet must be placed in the HEAD section of the View where the User must be prevented from going back.
<script type = "text/javascript" >
   function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
</script>
 
 
Disable Browser Back Button Implementation in ASP.Net MVC
For illustration purposes, two Views are used Login and Home. After Login User is sent to Home View and using Browser Back button he will be prevented from going back to Login View from Home View.
Login View
The HTML Markup of Login View consists of an HTML Anchor link to the Home View.
The Disable Browser Back Button Script is placed in the HEAD section so that User cannot access the Login View using Browser Back button from Home View.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <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/Home">Redirect to Home</a>
</body>
</html>
 
Home View
The HTML Markup of Home View consists of an HTML Anchor link to the Login View.
Note: HTML Anchor link is added to illustrate that the Login View will be prevented only using Browser Back button and not when accessed using other means.
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Home</title>
</head>
<body>
    <h3>Home</h3>
    <hr/>
    <a href="/Home/Login">Redirect to Home</a>
</body>
</html>
 
 
Screenshot
Disable Browser Back Button using JavaScript (jQuery) in ASP.Net MVC
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads