10

I am trying to understand the following jQuery Mobile example.

$( '#aboutPage' ).live( 'pageinit',function(event){
  alert( 'This page was just enhanced by jQuery Mobile!' );
});

What is #aboutPage in this context? What is the object pageinit is binding to?

Josh Unger
  • 6,717
  • 6
  • 33
  • 55
dev.e.loper
  • 35,446
  • 76
  • 161
  • 247

1 Answers1

13

aboutPage should be the id of the page.(i.e.div with data-role="page").live() attaches the funcion you have defined which contains thealert to the pageinit event of aboutPage.pageinit is triggered on a page when the page is initialized.

So in short What your code does is

it will execute the alert statement when aboutPage is initialized

The page might be initialized even if it is not in view.So even before you go to that page,the pageinit of the div will be triggered.If you are loading another html file as the new page pageinit for that page will be triggered only when you load that page into view.So,in your case if you want to do something when your div comes into view,you can try the pagebeforeshow and pageshow.pagebeforeshow will be triggered on the new page before animation starts and pageshow after animation is over.

user700284
  • 13,540
  • 8
  • 40
  • 74
  • Yup. That is what I guessed it would be and I tried hooking up pageinit event on div with data-role="page" but for some reason the method is not getting called the first time I transition to a page. When I hit refresh the method gets called. – dev.e.loper Oct 11 '11 at 17:58
  • Edited the answer to explain about this.Hope it helps. – user700284 Oct 12 '11 at 04:58
  • 3
    Figured it out. Every page had it's own javascript to hook up 'pageshow' events. However I was using $.mobile.changePage which only loads a div with data-role=page and nothing else on that page (including inline javascript) gets executed. Here is the answer that helped me http://stackoverflow.com/questions/7449402/jquery-mobile-mobile-changepage-not-loading-external-js-files/7449731#7449731 – dev.e.loper Oct 12 '11 at 13:17