Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Back Button in Browser using jquery?

Tags:

jquery

button

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

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

}

Please help me..

like image 404
user380979 Avatar asked Jul 14 '10 05:07

user380979


2 Answers

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);
        };
    });
like image 150
reza gholami Avatar answered Oct 05 '22 11:10

reza gholami


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;   
            }
        };
    }
};
like image 27
Nabha Narate - Shirkul Avatar answered Oct 05 '22 09:10

Nabha Narate - Shirkul