Prevent back and forward buttons in jQuery Mobile

Here is how you can prevent the back and forward (any other navigation not controlled by you) from occurring. 


$(document).on('pagebeforechange', function(event, object) {
   
    //Prevent back and forward buttons
    if (object.toPage && (typeof object.toPage==="string")) {
        var curHref = variableOfWhereIshouldBe;
        var toHrefHash = object.toPage.substr(object.toPage.indexOf("#"));
        if (curHref !== object.toPage && curHref !== toHrefHash){
            $.mobile.loading('hide');
            event.preventDefault();
            window.location = curHref;
        }
    }
   
});

Comments

Popular Posts