how to control back button event in Jquery mobile?

Serkan picture Serkan · Aug 13, 2013 · Viewed 95.9k times · Source

I tried to control back button but i cant.In here;

Take control of hardware back button jquery mobile

  event.keyCode == 27 //thats for escape 
  event.keyCode == 8 //thats for backspace..its also working on browser but it doesnt work on my tablet.

Any suggestions??

Answer

Omar picture Omar · Aug 13, 2013

Recommended method pagecontainerbeforechange: https://jqmtricks.wordpress.com/2014/12/01/detect-back-navigation/


You need to listen to navigation event and state.direction.

$(window).on("navigate", function (event, data) {
  var direction = data.state.direction;
  if (direction == 'back') {
    // do something
  }
  if (direction == 'forward') {
    // do something else
  }
});

jQM API: Navigation event

Demo