Disable Back Browser Button in web Page

This example will show how to disable Back button in your browser. To achieve this objective, the javascript will be use to redirect back to the current page if the user click on the browser back button and give message to user. So let see the example.

I'll set the script at HEAD element and use onpageshow and  onload event in the body element.

The Javascript put in Head element

<script type="text/javascript">
        window.history.forward();
        function noBack() { window.history.forward(); document.getElementById("Message").innerHTML = "You cannot Go Back"; }
    </script>


The Html/Aspx/Jsp page

:
: Head element
:

<body onpageshow="if (event.persisted) noBack();" onload="noBack();" >

:
: body content

 <div id="Message" style="color:red;">
 </div>

:
:
</body>
</html>


The Output

If the user click on the browser back button, the script will automatically redirect to the current page and give the message "You cannot go back" to user


Note : You can customize the script for example if you want to force logout user if user click on the back button, you can create one page "forceLogout.aspx"  and then edit the script to redirect page to forceLogout.aspx . So everytime user click on the browser back button , user will redirect to forceLogout.aspx page.

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example