Prevent Use of the Back Button (in IE)

前端 未结 15 1915
囚心锁ツ
囚心锁ツ 2020-11-27 21:16

So the SMEs at my current place of employment want to try and disable the back button for certain pages. We have a page where the user makes some selections and submits the

相关标签:
15条回答
  • 2020-11-27 21:36

    Do you have access to the server-side source code? If so, you can put a check on the first page that redirects to the second page if the information has been submitted already (you'll need to use sessions for this, obviously). At a former job, this is how we handled multi-step applications (application as in application for acceptance).

    0 讨论(0)
  • 2020-11-27 21:40

    Nah, you're doomed. Even if you pop the page up in some different browser and hid the back button, there's always the Backspace key.

    The problem with marketing guys and analyst types is that some of them do not understand the fundamental concept of the web being stateless. They do not understand that the page is totally, totally unaware of the browser using it and absolute control of the browser is totally outside the capability of web pages.

    The best way to discourage your users to hit the back button is to make sure that your page loses all its data when they press back, e.g., the comment page is the only point where the data can be saved, and if they do press the back button they have to do everything all over again (think along the lines of pragma: nocache).

    Users will complain, sure, but they are the reason that this godforsaken requirement exists, right?

    0 讨论(0)
  • 2020-11-27 21:44

    Disabling the back button seems kind of a "brute force" approach.

    Another option would be that you could jump out to a modal dialog that doesn't have command buttons, walk users through the workflow, and close the dialog when the process is complete.

    0 讨论(0)
  • 2020-11-27 21:45

    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";
    }
    // If you want to skip the auto-positioning at the top of browser window,you can add the below code:
    window.location.hash=' ';
    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 the back button!
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 21:45

    Could you move the comment to the previous page and make it a required field there?

    Disabling the back button will not work.

    0 讨论(0)
  • 2020-11-27 21:45

    There simply is no reliable way to do this. You cannot guarantee that 100% of the time you can stop the user from doing this.

    With that in mind, is it worth going to extremely exotic solutions to disable "most" of the time? That's for you to decide.

    Good luck.

    0 讨论(0)
提交回复
热议问题