In this article I will explain with an example, how to prevent user from navigate to previous page using Back Button in Browser or the Back option in the Context Menu using jQuery.
One cannot disable the Browser Back Button functionality only thing that can be done is prevent them going back.
 
 

Concept

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

Disable Browser Back Button using jQuery

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.unload function returns null.
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
    function preventBack() {
        window.history.forward();
    }
    setTimeout("preventBack()", 0);
    $(window).on("unload", function () {
        null;
    });
</script>
 
 

Disabling Browser Back Button using jQuery

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.
 

Disabling Browser Back Button Script

Inside the HTML Markup, first the following script file is inherited.
1. jquery.min.js
 
Finally, the script to disable the Browser Back Button is placed inside the HEAD section.
<html>
<head>
    <title>Login</title>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        function preventBack() {
            window.history.forward();
        }
        setTimeout("preventBack()", 0);
        $(window).on("unload", function () {
            null;
        });
    </script>
</head>
<body>
    <h3>Login</h3>
    <hr />
    <a href="Home.htm">Redirect to Home</a>
</body>
</html>
 

Home Page

The Home View consists of an HTML Anchor tag and its href property of the Anchor link is set to name of 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.
 
<html>
<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 jQuery
 
 

Browser Compatibility

The above code has been tested in the following browsers.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Demo

 
 

Downloads



Other available versions