Search code examples
jqueryjquery-mobile

How can I check if a page was reached via jQuery Mobile's back button?


I'm working on a jQuery page and I want to know if the page was reached via the user's "back" button (the one in jQuery mobile (data-rel="back"), not the browser back button).

I have done a bit of web searching and reading through jqm's site, but no joy.


Solution

  • Try with the callback state.direction

    LIVE DEMO

    more info here

    $(window).on("navigate", function (evt, data) {
      var backForw = data.state.direction;
      if (backForw) {
         console.log("Button "+ backForw +" was used");
      }
    });
    

    Or, if you want you can store a flag into localStorage and check for it's existence or using JS's pushState...

    https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
    https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history