1

I´m developing an app with jQuery Mobile and Phonegap where I need to check a variable before the first page shows up. It´s only 3 lines of code, where I check if a variable is true or false. If it´s true the user gets redirected to another page with mobile.changePage().

So is there a way to check this variable before the user gets to see the first page. (Must be executed between splashscreen disappears and first page shows up. Thx for your help

m1crdy
  • 1,371
  • 2
  • 25
  • 58
  • 1
    You need to listen to `pagebeforechange`. http://api.jquerymobile.com/pagebeforechange/ – Omar Oct 24 '13 at 20:50
  • 1
    @Omar correct answer. Thank you! if you post it as an answer i´ll check it as correct! – m1crdy Oct 28 '13 at 10:00

2 Answers2

1

You need to listen to pagebeforechange event and not pagebeforeshow. Because the first one fires before transition triggers and most importantly before updating browser's history.

  • pagebeforechange: Triggered twice during the page change cyle: First prior to any page loading or transition and next after page loading completes successfully, but before the browser history has been modified by the navigation process.

  • pagebeforeshow: Triggered on the "toPage" we are transitioning to, before the actual transition animation is kicked off.

Related post: https://stackoverflow.com/a/19522643/1771795

Demo

Community
  • 1
  • 1
Omar
  • 32,302
  • 9
  • 69
  • 112
0

Take a look at JQM's pagebeforeshow event. Documentation is here.

Omar
  • 32,302
  • 9
  • 69
  • 112
Dom
  • 2,569
  • 1
  • 18
  • 28
  • 1
    `pagebeforeshow` will result in showing the target page before redirecting. – Omar Oct 24 '13 at 20:49
  • @Omar so if you redirect before a page is shown it will still show the base page before the redirected page? I am going to have to test this later as that doesn't make sense. Not trying to be argumentative, truly trying to understand and help at the same time. – Dom Oct 24 '13 at 21:57
  • 1
    Fork this fiddle and use `pagebeforeshow`. http://jsfiddle.net/Palestinian/YaWpX/ more details here: http://stackoverflow.com/questions/19520101/stop-showing-page/19522643#19522643 – Omar Oct 24 '13 at 22:15
  • thx for reply. then pagebeforechange is the right event for me. But is this event fireing before the first page (index.html) shows up? – m1crdy Oct 25 '13 at 09:36
  • @Omar, took a look and you are right. My solution was based on a multipage site and using window.location to change page. I keep forgetting that the typical JQM site is a single page with multiple `
    ` elements.
    – Dom Oct 25 '13 at 10:56
  • @m1crdy yes, it fires before. like in this demo, it goes directly to page3 before showing the first page http://jsfiddle.net/Palestinian/Ea8Ya/ – Omar Oct 25 '13 at 11:00
  • @Dom multi-page and single-page are the same as jQM loads pages via Ajax. – Omar Oct 25 '13 at 11:02