Disable browser back button using jquery?

I would like to disable the back button when the user clicks on unload for this. I only delete this user's cookies, not the session in my application. And you want to disable the back button

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

}

      

Please help me..

+8


source to share


8 answers


you have to push your url to pushState and clear browser history:

try this:



$(document).ready(function() {
        window.history.pushState(null, "", window.location.href);        
        window.onpopstate = function() {
            window.history.pushState(null, "", window.location.href);
        };
    });

      

+9


source


This is another method to disable feedback features on any web page. We can turn off reverse navigation by adding the following code to the web page. Now the trick is that you have to add this code to all the pages where you want the user not to return from the previous page. For example, the user follows the navigation page1 -> page2. And you want to stop the user from page2 to go back to page1. In this case, all of the following code is on page 1.

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>

      

+4


source


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 in all browsers (except <IE10), even if the urls contain query strings.

You don't even need jquery for this.

look here to apply this to your page.

+3


source


Use the following code as :

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

      

+2


source


This isn't exactly your approach, but you can disable the navigation bar of any window using simple javascript. Just set window.menubar

and window.toolbar

visibility to false

like this:

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

      


Update:

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

So the only real way is to open a new window with a menu and a toolbar set to "none" 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 must also inherit the history of the window in which it was opened.

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

+1


source


Simple logic: move forward when pushing forward


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

      

Save this js code in your page.

0


source


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


source


Sunil was right, but to use jQuery, paste the code below on any page you want to prevent. I tested this in IE 11, chrome and FF which worked great.

$(document).ready(function () {
    function disableBack() {window.history.forward()}

    window.onload = disableBack();
    window.onpageshow = function (evt) {if (evt.persisted) disableBack()}
});

      

0


source







All Articles