How To Disable Browser Back Button In Asp.Net Using Javascript

How To Disable Browser Back Button In Asp.Net Using JavaScript

Today i am working on some project, in that i have requirement of disabling browser back button.To do so i searched Google and finally i got to know that we can do this by using JavaScript.You just have to include a JavaScript on your page HEAD section.

Code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="disable back button in browser.aspx.cs" Inherits="disable_back_button_in_browser" %>
<!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>Untitled Page</title>
<script type = "text/javascript" >
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Hi this is a test page</h1>
<a href ="index.html">Diable Back Button</a>
</div>
</form>
</body>
</html>
 


In this page when you click on Disable Back Button link, it will refresh the page instead of redirecting. You can also try this in HTML pages, it is working fine in HTML also.