in JQM pageshow works, pagebeforeshow doesn't

133 Views Asked by At

hi I have some jQuery Mobile and pageshow works fine (it's blip bugged but it works fine) like this:

$(document).off('pageshow').on('pageshow', function () { 
   console.log('it happened');
});

NB: I have had numerous (READ: weeks lost) problems with caching, so I am afraid despite all information to the contrary the off() has to stay. You will argue with me, I will keep off() and I will be right.

ok but when I change it to

$(document).off('pagebeforeshow').on('pagebeforeshow', function () { 
   console.log('it happened');
});

it doesn't run. Now I know that you are supposed to be doing something like

$(document).off('pagebeforeshow').on('pagebeforeshow', '#somediv', function () { 
   console.log('it happened');
});

but I don't want it to be on just that div I want it to be on the whole document.

1

There are 1 best solutions below

0
On

You can test when you leave a specific page like this ( worked in my case ):

--> Start a loop function ( setInterval ) that tests every 2 sec if you are still on that page, and if not ( it means that you just left the page ) execute only once a function

Here is the code:

<script>$('#YOUR_PAGE').bind('pageshow', function(data) { 
      var repeat = setInterval(function() {
      var page_path = $.mobile.path.parseUrl(window.location).toString().toLowerCase() ;
      var page_page = page_path.split("#"); 
          if ( page_page[1]  != "YOUR_PAGE"){

          ...
          DO YOUR THING
          ...

          clearInterval(repeat);
               }
          }, 2000);
      });
</script>