1

I'm working on a jQuery mobile project and need to prevent the user from getting back to certain pages, the UI obviously doesn't provide a back button where the user is not supposed to go back, but I still have a problem when the user clicks the back button on the browser.

I was trying to prevent the user from going back by binding a pagebeforechange event to a function which, by looking at the URL of the destination page could know if the user is going back. This is kind of simple in my project since there are only 2 URLs accesible via the UI so any other URL would mean the user hit the back button. I'm using a regex due to the nature of the URL to validate this:

$( document ).bind('pagebeforechange', function(event, data) {  

  if(typeof data.toPage === "string") {
    var url = data.toPage;
    var regex = /.../
    var paramsIndex = url.indexOf('?'); // Get rid of possible params

    if(paramsIndex > 0) {
      url = url.substr(0, paramsIndex);
    }

    if(regex.test(url)) {   
       event.preventDefault(); // Prevent the user from going back..
    }       
  }

});

Unfortunately this doesn't work, even though the regex is working properly and matching everytime the user hits the back button (and only then). I was wondering if you have any ideas, I'd prefer to keep using the ajax navigation of jquery mobile.

Cheers

Manuel Martinez
  • 388
  • 1
  • 3
  • 13

2 Answers2

0

I'm not a specialist of mobile developement, but seeing this question, maybe you should try this: in your event handler, replace event.preventDefault(); with return false;. This can only give you more chances to achieve your goal.

Community
  • 1
  • 1
Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90
0

return false; as Samuel said or add event.stopImmediatePropagation();

Peter Tseng
  • 13,613
  • 4
  • 67
  • 57