Tuesday, August 18, 2015

How to detect back navigation in mobile

    The use of pagecontainerbeforechange  it is navigate, hashchange and popstate all in one event! Whether browser’s back button or $.mobile.back() is used, pagecontainerbeforechange is what you need.
      It event fires twice on each page change, first time it returns toPage as a string, and second time as an object. To alter toPage value, it should be a string, so you need to do changes on first time it fires.
      Navigation direction can be obtained from options.direction, it can be back or forward.

Code : 

$(document).on("pagecontainerbeforechange", function (e, data) {
    if (typeof data.toPage == "string" && data.options.direction == "back" && data.prevPage[0].id == "PageX") 
{
        data.toPage = "#pageY"; /* redirect to pageY */
        data.options.transition = "flip"; /* optional */
    }
});
  

Popular Posts