Disable browser's back button

后端 未结 20 1441
猫巷女王i
猫巷女王i 2020-11-22 04:27

How to disable browser\'s BACK Button (across browsers)?

相关标签:
20条回答
  • 2020-11-22 05:19

    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 the back button!
    </body>
    </html>
    

    What is it doing?

    From Comments:

    This script leverages the fact that browsers consider whatever comes after the "#" sign in the URL as part of the browsing history. What it does is this: When the page loads, "#1" is added to the URL. After 50ms the "1" is removed. When the user clicks "back", the browser changes the URL back to what it was before the "1" was removed, BUT - it's the same web page, so the browser doesn't need to reload the page. – Yossi Shasho

    0 讨论(0)
  • 2020-11-22 05:20

    While i'm looking for the answer myself, "Best Practice" is.... outdated... Just like browsers are.(Really browsers are ugly fossils)

    The best/safest solution would be for browsers to implement a method/request where the user can grant the page the ability to control the interface.

    Why? Because for my current project i'm building a 100% JavaScript built and controlled interface.. And back button's have no place in my project since there is no page change. (Ie bloody fast and no page-flashes because of a refresh.. Just like a real application!)

    I know why the ability to "highjack" the interface isn't there, and i understand it. But atleast we should have the ability to request it from the browser! Now that would truly be "best practice" without the highjack dangers.

    But browsers being browsers.. I don't expect anything exiting to happen in this regard.

    0 讨论(0)
  • 2020-11-22 05:21

    I was searching for the same question and I found following code on a site. Thought to share it here:

    function noBack()
    {
       window.history.forward()
    }
    noBack();
    window.onload = noBack;
    window.onpageshow = function(evt){ if(evt.persisted) noBack(); }
    window.onunload = function(){ void(0); }
    

    However as noted by above users, this is never a good practice and should be avoided for all reasons.

    0 讨论(0)
  • 2020-11-22 05:22

    Do not disable expected browser behaviour.

    Make your pages handle the possibility of users going back a page or two; don't try to cripple their software.

    0 讨论(0)
  • 2020-11-22 05:23

    This question is very similar to this one...

    You need to force the cache to expire for this to work. Place the following code on your page code behind.

    Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    
    0 讨论(0)
  • 2020-11-22 05:23

    Instead of trying to disable the browser back button it's better to support it. .NET 3.5 can very well handle the browser back (and forward) buttons. Search with Google: "Scriptmanager EnableHistory". You can control which user actions will add an entry to the browser's history (ScriptManager -> AddHistoryPoint) and your ASP.NET application receives an event whenever the user clicks the browser Back/Forward buttons. This will work for all known browsers

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