Disable browser's back button

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

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

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

    Try this code. Worked for me. It basically changes the hash as soon as the page loads which changes recent history page by adding "1" on URL. So when you hit back button, it redirects to same page everytime.

     <script type="text/javascript">
        var storedHash = window.location.hash;
        function changeHashOnLoad() { window.location.hash = "1";}
        window.onhashchange = function () {
            window.location.hash = storedHash;
        }
    </script>
    
    <body onload="changeHashOnLoad(); ">
    
    </bod>
    
    0 讨论(0)
  • 2020-11-22 05:02
    <body onLoad="if(history.length>0)history.go(+1)">
    
    0 讨论(0)
  • 2020-11-22 05:04

    There have been a few different implementations. There is a flash solution and some iframe/frame solutions for IE. Check out this

    http://www.contentwithstyle.co.uk/content/fixing-the-back-button-and-enabling-bookmarking-for-ajax-apps

    BTW: There are plenty of valid reasons to disable (or at least prevent 1 step) a back button -- look at gmail as an example which implements the hash solution discussed in the above article.

    Google "how ajax broke the back button" and you'll find plenty of articles on user testing and the validity of disabling the back button.

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

    If you rely on client-side technology, it can be circumvented. Javascript may be disabled, for example. Or user might execute a JS script to work around your restrictions.

    My guess is you can only do this by server-side tracking of the user session, and redirecting (as in Server.Transfer, not Response.Redirect) the user/browser to the required page.

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

    You should be using posts with proper expires and caching headers.

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

    This seems to have worked for us.

    history.pushState(null, null, $(location).attr('href'));
    window.addEventListener('popstate', function () {
        history.pushState(null, null, $(location).attr('href'));
    });
    
    0 讨论(0)
提交回复
热议问题