Pages

Thursday, December 29, 2011

Disable Browser Back Button

It is a common question for developer how to to prevent user’s from going to previous page using Browser Back button.
You can do it in various way:


Javascript Codesnippet1:


function disableBackButton()
{window.history.forward();
}
setTimeout("disableBackButton()", 0);




For internet explorer: onload="disableBackButton()">
If you are using firefox then use onunload="disableBackButton()"> instead of onload.


For Server side:
C#-> Asp.net 2.0

ASP.NET 2.0 (there is a new HttpCachePolicy class) :

   /*
    * Code disables caching by browser. Hence hitting the back browser button
    * causes the Page_Load event to fire again.
    */
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now); //or a date much earlier than current time

No comments: