Disable Back Button in Browser using jquery?

后端 未结 8 654
慢半拍i
慢半拍i 2020-12-03 05:55

I would like to Disable Back button when user click on logoff for this i delete only cookies of that user not session in my application. And want to disable Back button

相关标签:
8条回答
  • 2020-12-03 06:23

    This is not exactly your approach, but you may disable the navigation panel of any window using plain javascript. Just set window.menubar and window.toolbar visibility to false as follows,

    window.menubar.visible = false ;
    window.toolbar.visible = false ;
    

    Update:

    Changing the menubar and toolbar visibility of an existing window seems to violate security protocols. (https://developer.mozilla.org/en/DOM/window.menubar)

    Therefore the only real way is to open a new window with menubar and toolbar set to "no" in the first place:

    window.open(url,name,"menubar=no,toolbar=no[, additional options]",true) ;
    

    If you set the replace argument (the last argument) to true the new window should also inherit the history of the window it was opened in.

    Check https://developer.mozilla.org/en/DOM/window.open and http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx for reference.

    0 讨论(0)
  • 2020-12-03 06:24

    Use following code as it is:

    window.onload = function () {
        if (typeof history.pushState === "function") {
            history.pushState("jibberish", null, null);
            window.onpopstate = function () {
                history.pushState('newjibberish', null, null);           
            };
        }
        else {
            var ignoreHashChange = true;
            window.onhashchange = function () {
                if (!ignoreHashChange) {
                    ignoreHashChange = true;
                    window.location.hash = Math.random();                
                }
                else {
                    ignoreHashChange = false;   
                }
            };
        }
    };
    
    0 讨论(0)
  • 2020-12-03 06:24

    This will work, it worked for me as I am dealing with mobile browsers, android and ios.

    window.history.forward();
    window.onload = function()
    {
      window.history.forward();
    };
    
    window.onunload = function() {
      null;
    };
    
    0 讨论(0)
  • 2020-12-03 06:27

    you must push your url in pushState and clean the browser history:

    try this :

    $(document).ready(function() {
            window.history.pushState(null, "", window.location.href);        
            window.onpopstate = function() {
                window.history.pushState(null, "", window.location.href);
            };
        });
    
    0 讨论(0)
  • 2020-12-03 06:28

    Simple logic: move forward when click forward


    function preventBack() {
        window.history.forward();
    }
     window.onunload = function() {
        null;
    };
    setTimeout("preventBack()", 0);
    

    Keep this js code in your page it works.

    0 讨论(0)
  • 2020-12-03 06:29

    Sunil was correct, but to use jQuery paste the code below into any page that you want to prevent going back too. I tested this in IE 11, chrome and FF, which worked fine.

    $(document).ready(function () {
        function disableBack() {window.history.forward()}
    
        window.onload = disableBack();
        window.onpageshow = function (evt) {if (evt.persisted) disableBack()}
    });
    
    0 讨论(0)
提交回复
热议问题