Disabling Back button on the browser

44,111

Solution 1

It's not possible, sadly. However, consider your applications navigation model. Are you using Post/Redirect/Get PRG Model? http://en.wikipedia.org/wiki/Post/Redirect/Get?

This model is more back button friendly than the Postback model.

Solution 2

You shouldn't.

You could attach some script to the onbeforeunload event of a page and confirm with the user that's what they want to do; and you can go a bit further and try to disable it but of course that will only work for users who have javascript turned on. Instead look at rewriting the app so you don't commit transactions on each page submit, but only at the end of the process.

Solution 3

I strongly urge you to go to heroic lengths to prevent breaking the back button, it is a sure fire way to alienate your users and even made it to No.1 on Jacob Neilsen's Top 10 Web Design Mistakes in 1999.

Perhaps you could consider rather asking the question: "How to avoid breaking the back button for <insert your scenario here>?"

If Scott's answer hits close to the mark, consider changing your flow to the PRG model. If it's something else, then give a bit more detail and see how we can help.

Solution 4

I came up with a little hack that disables the back button using JavaScript. I checked it on chrome 10, firefox 3.6 and IE9:

<!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" >
<title>Untitled Page</title>
<script type = "text/javascript" >
function changeHashOnLoad() {
     window.location.href += "#";
     setTimeout("changeHashAgain()", "50"); 
}

function changeHashAgain() {
  window.location.href += "1";
}

var storedHash = window.location.hash;
window.setInterval(function () {
    if (window.location.hash != storedHash) {
         window.location.hash = storedHash;
    }
}, 50);


</script>
</head>
<body onload="changeHashOnLoad(); ">
Try to hit back!
</body>
</html>

Solution 5

Best option is not to depend on postbacks to control flow, however if you are stuck with it (for now)

you may use something like this:

  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.Cache.SetExpires(Now.AddSeconds(-1));
  Response.Cache.SetNoStore();
  Response.AppendHeader("Pragma", "no-cache");

Soon you will find that it will not work on all browsers, but then you may introduce a check in your code like:

 if (Page.IsPostBack)
 {
        if (pageIsExpired()){
           Response.Redirect("/Some_error_page.htm");
        }
        else {
           var now = Now;
           Session("TimeStamp") = now.ToString();
           ViewState("TimeStamp") = now.ToString();
        }

  private boolean pageIsExpired()
  {
     if (Session("TimeStamp") == null || ViewState("TimeStamp") == null)
        return false;

     if (Session("TimeStamp") == ViewState("TimeStamp"))
        return true;

        return false;
  }

That will solve problem to some extend, Code not checked -- only for examples purposes..

Share:
44,111
Karthik Rao
Author by

Karthik Rao

Debugger is my best friend.

Updated on October 13, 2020

Comments

  • Karthik Rao
    Karthik Rao over 3 years

    I am writing an application that if the user hits back, it may resend the same information and mess up the flow and integrity of data. How do I disable it for users who are with and without javascript on?