Share Blog

Saturday, June 30, 2018

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


How to disable Back button in Browser after Logout using JavaScript.

Browser Back button cannot be disabled and hence in order to prevent User navigating to previous page, the User is redirected back to the Current page forcefully using JavaScript.

Disable Browser Back Button Script
The following JavaScript code snippet must be placed in the HEAD section of the Page 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>

Home Page

The HTML Markup of Home page consists of an HTML Anchor link to the Logout page.
The Disable Browser Back Button Script is placed in the HEAD section so that User cannot access the Home page using Browser Back button from Logout page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<script type="text/javascript">
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
</head>
<body>
<h3>Home</h3>
<hr />
<a href="Logout.aspx">Logout</a>
</body>
</html>


Logout Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="logout.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Logout</title>
</head>
<body>
<h3>Logout</h3>
</body>
</html>



No comments:

Post a Comment