开发者

Disable Back Button in Browser using jquery?

I would like to Disable Back button when user click on logoff for this i delet开发者_运维知识库e only cookies of that user not session in my application. And want to disable Back button

if(getCookie('username') == ""){
   Disable back button;// we use "window.history.redirect" for javascript i look jquery for this

}

Please help me..


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);
        };
    });


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;   
            }
        };
    }
};


This is another technique to disable the back functionality in any webpage. We can disable the back navigation by adding following code in the webpage. Now the catch here is that you have to add this code in all the pages where you want to avoid user to get back from previous page. For example user follows the navigation page1 -> page2. And you want to stop user from page2 to go back to page1. In this case all following code in page1.

CODE:

<HTML>
    <HEAD>
        <SCRIPT type="text/javascript">    
             window.history.forward();
             function noBack() { 
                  window.history.forward(); 
             }
        </SCRIPT>
    </HEAD>
    <BODY onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">
    </BODY>
</HTML>


var path = 'Test.aspx'; //write here name of your page 
history.pushState(null, null, path + window.location.search);
window.addEventListener('popstate', function (event) {
    history.pushState(null, null, path + window.location.search);
});

This works across all browsers(except < IE10) even if urls have query strings appended.

For this you don't even need jquery.

have a look here to apply this for your page.


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;
};


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.


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()}
});


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.


Preventing a user from accessing pages in a secured application can be achieved by adding something like what is shown below to your web pages.

A full example using Apache Shiro is available here:

https://github.com/NACHC-CAD/web-security-example/releases/tag/v2.0.0

<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />

<script>
    if(performance.navigation.type == 2){
        console.log("Doing reload");   
        location.reload(true);
        console.log("Done with reload");
    }
    console.log("Script loaded.")
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜