简体   繁体   中英

how to control back button event in Jquery mobile?

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??

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

You can do this without Jquery mobile

 window.addEventListener("hashchange", function(e) { if(e.oldURL.length > e.newURL.length) alert("back") }); 
 <a href="#p2">goto page 2</a> and then use browser's navigation.</div> 

And also Demo in codepen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM