Article

Disable Browser Back Button Using JavaScript in asp.net

09 Feb 2018 Naresh Chandra
1 Comments 760 Views



We need to disable browser back button in some case like after login, after logout or redirect any pages. Here Naresh Chandra explain how to disable browser back button using javascript.

 

Put below javascript code in the header section of your login page.

<script type="text/javascript">
        window.onload = function () {
            setTimeout("preventBackLogin()", 0);
        };
        function preventBackLogin() {
            window.history.forward();          
        }
        window.onunload = function () { null };
</script>

After redirect to home page you will be unable to use browser back button.

HTML Markup

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="test.Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login</title>
    <script type="text/javascript">
        window.onload = function () {
            setTimeout("preventBackLogin()", 0);
        };
        function preventBackLogin() {
            window.history.forward();          
        }
        window.onunload = function () { null };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSubmit" runat="server" Text="Sent To Home Page" 
            onclick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

Redirection Code

 protected void btnSubmit_Click(object sender, EventArgs e)
 {
     Response.Redirect("Home.aspx");
 }

 

Naresh Chandra

Hi, I have 7 years of experience in .NET techologies

Comments

No coments found to display!

Leave a Comment