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

55 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Worked for me!!! Thanks.

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

    ReplyDelete
  4. @happy:

    Which browser your are using ?

    Please paste ur code here so that i can look into

    ReplyDelete
  5. 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...

    ReplyDelete
  6. 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

    ReplyDelete
  7. @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 ?

    ReplyDelete
  8. 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

    ReplyDelete
  9. @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();
    }

    ReplyDelete
  10. 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

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

    Thanks,
    Sumeet

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

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. @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 ?

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

    ReplyDelete
  17. @Above:

    For detecting page refresh read my article mentioned below

    Detect Page refresh in ASP.NET

    ReplyDelete
  18. 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

    ReplyDelete
  19. 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...

    ReplyDelete
  20. 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...

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

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

    can we?

    ReplyDelete
  23. Thanks it works....

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

    ReplyDelete
  25. Thanks for this article

    ReplyDelete
  26. 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.

    ReplyDelete
  27. 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..

    ReplyDelete
  28. Thanks it helped me. Appreciation to the developers.

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

    ReplyDelete
  30. its working.. thank you so much..

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

    ReplyDelete
  32. 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

    ReplyDelete
  33. 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

    ReplyDelete
  34. tthhanxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    ReplyDelete
  35. this doesnot work in IE 7.

    ReplyDelete
  36. Thanks a lot.I wanted it for my project

    ReplyDelete
  37. thanks it worked!!!

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

    ReplyDelete
  39. hi,
    it is working only with IE...

    ReplyDelete
  40. 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 ...

    ReplyDelete
  41. how to use this in blogspot?


    my blog is

    http://howtohackthe.blogspot.com

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

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

    ReplyDelete
  44. 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

    ReplyDelete
  45. @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

    ReplyDelete
  46. 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...

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

    ReplyDelete
  48. 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!!! :)

    ReplyDelete
  49. Its Working!!!!!!!
    Thank You Very Much!

    ReplyDelete
  50. 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?

    ReplyDelete
  51. 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

    ReplyDelete
  52. Thanks its working,,,,,

    ReplyDelete