Disable Browser Back Button Using Javascript ASP.NET

This example explains how to Disable Browser Back Button Using Javascript In ASP.NET. to avoid user going to previous page by clicking on back button of browser, for this we need to use javascript to prevent user navigating to previous page by hitting back button.

Disable Browser Back Button Using Javascript
Just put this javascript on the html section of aspx page above head section

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


We need to put it on the html section of the page which we want to prevent user to visit by hitting the back button

Complete code of the page looks like this
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>
</head>
<body onload="disableBackButton()">
<form id="form1" runat="server">
<div>
This is First page <br />
<br />
Go to Second page
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
PostBackUrl="~/Default2.aspx">Go to Second Page
</asp:LinkButton></div>
</form>
</body>
</html>

If you are using firefox then use <body onunload="disableBackButton()"> instead of onload

If you want to disable back button using code behind of aspx page,than you need to write below mentioned code

C# code behind
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string strDisAbleBackButton;
strDisAbleBackButton = "";
ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
} 

VB.NET code behind
Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
    MyBase.OnPreRender(e)
    Dim strDisAbleBackButton As String
    strDisAbleBackButton = ""
    ClientScript.RegisterClientScriptBlock(Me.Page.[GetType](), "clientScript", strDisAbleBackButton)
End Sub


We can also achieve this by disabling browser caching or cache by writing this line of code either in Page_load event or in Page_Init event

protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}
Doing this,user will get the page has expired message when hitting back button of browser


Have fun

Download the sample code attached


Other articles on javascript:
1. Highlight gridview row on mouse over using javascript in asp.net and C# c-sharp

2. Disable copy paste cut and right click in textbox on aspx page using javascript

3 .Mozilla firefox JavaScript window.close() not working / does not work in firefox
If you like this post than join us or share

56 comments:

mohsin said...

Hello
A good post with wonderful ideas

www.supirscience.blogspot.com

www.mnewsalert.blogspot.com


Arifur said...

This comment has been removed by the author.


Anonymous said...

Worked for me!!! Thanks.


happy said...

Not working ...I am still able to browse back..


amiT jaiN said...

@happy:

Which browser your are using ?

Please paste ur code here so that i can look into


Anonymous said...

hi :) thanks for this post. I want to disable the browser catching through c# code and have tried what you have suggested but it is not working... the javascript code you have suggested is working fine but i want to disable browser catching. I am using IE7. The tool i am using is visual studio 2005, asp.net 2.0... please help me...


Anonymous said...

Hi Amit,

I have tried both of your solutions however none of them is working for me. I am able to use 'Back' button while i m on the second page.

What I am doing is, I created a simple HTML/JS page with one hyperlink to another dummy page with just text message in it. Now, while I am on the second page, my browser does "Allow" me to move back by clicking on the 'Back' button.

I tried C# version by pasting the C# code in page load as well as init of second page, but no success.

Can you help me?

Thanks,
Sumeet


amiT jaiN said...

@sumeet:

Which browser u r using ? if you are using firefox than u need to write onunload instead of onload

Also check whether javascript is enabled in your browser or not ?

I've checked the code again and it's working fine for me in IE or Firefox

Check this sample code

And do let me know whether it works for you or not ?


Anonymous said...

Amit, Thanks it worked last time after writing to you.

I am using IE6 JS is enalbed, can you tell me how can I make a page 'Do not store in cache' using C#? I am also using .Net 3.5 Framework


amiT jaiN said...

@Above:

Code mentioned below is used for disabling browser cache in ASP.NET


protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}


Anonymous said...

Do you know any way by which I can trap the Back button when it is clicked so that I can show an alert message to the user.

Also by using above code I have noticed that although the page does not reside in Temporary folder however, is there any possibility that I can also deny it from storing in the "Drop down" of BACK button??

Please suggest.


Thanks,
Sumeet


Anonymous said...

Is there any way I can trap Browser BACK button (IE6 is my browser)) and do something else on it?

Thanks,
Sumeet


prairana said...

wat about vb.net....if i hav to do same work wid it....plz help me out!


prairana said...

This comment has been removed by a blog administrator.


Anonymous said...

This comment has been removed by a blog administrator.


amiT jaiN said...

@prairana:

I've updated the code in article on how to disable back button using C# or VB.NET code behind.
Read it again and let me know if it solves ur query ?


Anonymous said...

Thanks :)


Anonymous said...

how can i disable the refresh button using c#.net?


amiT jaiN said...

@Above:

For detecting page refresh read my article mentioned below

Detect Page refresh in ASP.NET


Anonymous said...

Hi amit, I was used ur code ...But i need ur help... i used ur code behind in C# after session expire on LogOut button click event.. like that
logOut_click....
{
Session["UserName"]="";

used ur code behind code in C#

ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);

}


But i had a error on ClientScript

How can solve this problem


sam said...

Hi amit, I used ur C# code behind code in log out button click event....

but i got error in line no 8. i.e ClientScript

How i can solve this error.. I used internet explorer 6.0 Pls help me...


sagar said...

Hi amit, I used ur C# code behind code in log out button click event....

but i got error in line no 8. i.e ClientScript

How i can solve this error.. I used internet explorer 6.0 Pls help me...


Panduranga said...

How do I do disable the back button in classic ASP? I am a beginner in classic ASP. New to .NET also.


Anonymous said...

I want the back and forward button in disabled states only like when we open the browser for first time.

can we?


Anonymous said...

Thanks it works....


Anonymous said...

Hi Jay Hr Thnx a lot buddy it did work.My email jayvengs@gmail.com


Anonymous said...

Thanks for this article


tomb69 said...

Amit,
What if the page you are trying to disable the back button for is just a content place holder on a master page, and the body tag is located in the master page. I don't want to disable the back button on my entire site, just when the user submits a specific request.


Anonymous said...

Hii.. I m Pooja..
Your code will not work..
My can't disable my back button using javascript code..
plz help me to desable back button
as early as possible..


Abdullah said...

Thanks it helped me. Appreciation to the developers.


true said...

Good Job,and thank all of you that post code like this~~~~


ABDUL said...

its working.. thank you so much..


Anonymous said...

see this http://sintaks.blogspot.com/2010/08/javascript-disable-functionality-of.html


Khachatur Petrosyan said...

It works great. But i want to know how to do it in php because i am developing an application where i need this point.

browsers rendering


Anonymous said...

code works..thanks.
I disable the back button when user click on logout and it works.but when user click on other links after login then also he is unable to go back.
thnks in advance


Robin Thomas said...

The following post describes a few more methods to disable browser back button..
http://www.robinthomas.in/dotnet/restrict-user-go-back-to-previous-page-after-signout/


Anonymous said...

tthhanxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


Anonymous said...

this doesnot work in IE 7.


Anonymous said...

Thanks a lot.I wanted it for my project


Arun Sunny said...

thanks it worked!!!


Anonymous said...

Thank you very much. It helped me a lot. Thank you once again.


Anonymous said...

hi,
it is working only with IE...


Anonymous said...

Thanks Amit.


Anonymous said...

hi iam subbarao...

In my web application when i clicked logout button its redirecting to login page but when i clicked on back button in browser its going back to the pages which should appear after login.I should Prevent it according to my application.

my log out button code is
Button_click()
{
Session.abondon();
Response.Redirect("~LoginPage.aspx");
}
please help me ...


jomon johnson said...

how to use this in blogspot?


my blog is

http://howtohackthe.blogspot.com


Anonymous said...

Thanks the javascript solution worked and fixed my issue in all pages of the site.


Anonymous said...

This is idea works well in google but failed in Internet Explorer 9, Firefox 9 etc


abhishek said...

In my web application when i clicked logout button its redirecting to login page but when i clicked on back button in browser its going back to the pages which should appear after login.I should Prevent it according to my application.

my log out button code is
Button_click()
{
Session.abondon();
Response.Redirect("~LoginPage.aspx");
}
please help me ...
and send me java script


abhishek said...

.........


amiT jaiN said...

@Abhishek: Configure and use Login control with asp.net membership provider in your application to deny anonymous access to your website.

Read Login Page Using Login Control Example In Asp.Net to know how to configure asp.net membership provider and login control


Anonymous said...

Hi..I have a master page for my application. Other pages are ajax implemented. somehow this code is not working for me i have placed this code in the master page..so that it will apply to all the other pages is that right ?...could you please let me if there is a workaround...


Anonymous said...

thanks a lot . the code worked for me . i am using chrome browser..


Alakh Kaushik said...

hi i am using your code at the page load event of my login page....

protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}


on the click of log out button code i have added the code :-
protected void Log_Out_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("~/Log_In.aspx");
}



but....after being re-directed to the login page.....back button is still working....


pls help....!!!

thenx fo all your efforts!!! :)


Anonymous said...

Its Working!!!!!!!
Thank You Very Much!


Anonymous said...

Hi sir,
I want to use this functionality in html pages,
i am using onbeforeunload() function on html page.
Its not working with this function.
Any suggestion?


Anonymous said...

Thanks I have used the code to prevent the user to go to back to previous page, but when I used master page and placed the code in that master page I am unable to navigate among pages that are in herited from the master page. How can I solve this with this problem Please do specify answer

Thanks
Ganesh


Find More Articles