1

hi i am using jqueryMobile SPA template for my phone-gap app.

For web version my requirement is to disable or show warning message to user on click of browser back button. I googled my requirement but didn't find any desired solution.Please guide me . Thanks.

Varun Nayyar
  • 887
  • 4
  • 20
  • 46
  • maybe this post has info you can use http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch – Marvin Smit Oct 28 '14 at 07:38
  • It's not possible to disable it, in fact, you shouldn't. – Omar Oct 28 '14 at 10:19

2 Answers2

1

Try this- FIDDLE

$(window).on("navigate", function (event, data) {
    var direction = data.state.direction;
    if ( !! direction) {
        alert(direction);
    }
});

Please refer following link.

Refer This

Or you can use

window.history.forward(1);

when back button get pressed. But it loads and then redirected not proper way.

Suhas Gosavi
  • 2,170
  • 19
  • 40
1

To disable Backbutton of browser i used

 $(window).on("navigate", function (event) {
     event.preventDefault();
     window.history.forward(1);

});

But on pressing f5 it was creating error and redirecting me to login page.Thus i disable f5 ,ctrl+R button.

 var ctrlKeyDown = false;
 $(document).on("pagecreate", function (){    
    $(document).on("keydown", keydown);
    $(document).on("keyup", keyup);
 });

 function keydown(e) { 
   if ((e.which || e.keyCode) == 116 || ((e.which || e.keyCode) == 82 && ctrlKeyDown))
    {
     // Pressing F5 or Ctrl+R
     e.preventDefault();
    } else if ((e.which || e.keyCode) == 17) {
    // Pressing  only Ctrl
    ctrlKeyDown = true;
  }
}

function keyup(e){
  // Key up Ctrl
 if ((e.which || e.keyCode) == 17) 
    ctrlKeyDown = false;
}

It completely fulfilled my requirement.Thanks to Suhas :) .

Varun Nayyar
  • 887
  • 4
  • 20
  • 46
  • You may need to push an initial state to make this work... Something like this `window.onload = function() { if (window.history && history.pushState) { history.pushState(null, null, null); } };` – Micheal C Wallas Apr 11 '19 at 20:31