How do disable back button in browser

Generally there are cases when we want to disable back button in browser. We have multiple options to disable back button in browser. Here are the few:-

Java Script Way
We can use following java script to prevent back button:-
<script type = "text/javascript" >
function disableBackButton()
{ 
window.history.forward();
}
setTimeout("disableBackButton()", 0);
window.onunload=function()
{
null
};
</script>

HTML Way
We can also add Meta Tags in Head Section like this:-
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />

If you want to do above approach through code. Here is the trick:-

HtmlMeta pragma = new HtmlMeta();

pragma.HttpEquiv = "Pragma";

pragma.Content = "no-cache";

Page.Header.Controls.Add(pragma);

 

HtmlMeta expires = new HtmlMeta();

expires.HttpEquiv = "Expires";

expires.Content = "-1";

Page.Header.Controls.Add(expires);


ASP.Net Way
In ASP.Net we can use Cache to disable back button like this:-

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));

Response.Cache.SetNoStore();

Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

4 comments:

  1. Does this code work on every browser?

    ReplyDelete
  2. ssk,

    code posted in JavaScript works on all browsers

    ReplyDelete
  3. What's up friends, fastidious piece of writing and good arguments commented here, I am truly enjoying by these.

    Feel free to surf to my web-site; acoustic guitar chord

    ReplyDelete
  4. I don't even understand how I finished up here, however I believed this publish was once great. I don't recognize who you're but definitely you are going to a famous blogger when you aren't
    already. Cheers!

    My weblog ... http://www.carquack.com

    ReplyDelete