views:

4374

answers:

9

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?

+41  A: 

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.

Scott Hanselman
Nothing sad about it.
Joel Coehoorn
Valid point. I'm just sad. ;)
Scott Hanselman
Scott - Is the ability to disable the toolbars and right click menu IE specific then? I've seen LOB apps that completely lock down the navigation, but (again sadly) they also require the user have IE.
Jess
Well, some apps can run themselves in "Kiosk" mode, but that involves launch IE with command line switches.
Scott Hanselman
@Jess: That's one of those annoyances that good browsers try to fix, like unrequested pop-up windows. Firefox got a "Disable or replace context menu" setting for this reason.
Álvaro G. Vicario
+8  A: 

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.

blowdart
Hmmm... the onbeforeunload event would be called also when navigating away from the page because user is simply going to another page (not only because user pushed the back button), and the script would be called.
Marco Demajo
A: 

You could post the data on each form to a _NEW window. This will disable the back button on each window, but without javascript it might be difficult to force the old one closed.

skamradt
+2  A: 

Here's a previous post on it: http://stackoverflow.com/questions/54539/prevent-use-of-the-back-button-in-ie

Kevin
well, the recommendation engine didn't show it as I created the post :(
Haoest
+3  A: 

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.

fd
He did not say he wanted to "break" the back button. He wants to avoid a common pitfall where modern Ajax design patterns fail because a user presses "Back" meaning "Undo The Thing I Just Did With Your Ajax App" and instead gets "Go Back One Arbitrary And Ambiguous HTTP Request".
pcorcoran
To me that just reads "he didn't want to break the back button, he just wanted to break the back button to prevent it being broken". :SIt is possible to get AJAX/JavaScript and back buttons to play nice, all I'm suggesting is that would be the route I'd recommend following.
fd
+3  A: 

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

Claus Thomsen
A: 

4 Guys from Rolla wrote this article on disabling the back button a long time ago (in a galaxy far far away): http://www.4guysfromrolla.com/webtech/111500-1.shtml

torial
+2  A: 

Whatever you come up with to disable the back button might not stop the back button in future browsers.

If its late in the development cycle I suggest you try some suggestions above but when you get time you should structure your flow so that the back button does not interfere with the logic of your site, it simply takes the user back to the previous page like they expect it to do.

Keith
A: 

If an application is for internal use then there isn't anything wrong with wanting to disabling the back button. I think people that say 'you should't do it' instead of answering the question are just don't know how to do it and want to sound smart.