jQuery Mobile Dialog on page load

前端 未结 3 1616
無奈伤痛
無奈伤痛 2020-11-30 09:49

Working on my first jQuery Mobile app. There is a localStorage value that must have a value throughout the application, so I tapped into the pageshow event to check this val

相关标签:
3条回答
  • 2020-11-30 10:10

    I used "pagecreate" and that seems to solve my problem (so far...)

    $(document).on('pagecreate', "#page-formyID", function () {
    
    //whatever
    
    });
    
    0 讨论(0)
  • 2020-11-30 10:12

    Set a time interval to show the dialog, rather than call it once the page is shown.

    $(document).on('pageshow', '#myPage' ,function () {
     if (getValue() == null) {
      setTimeout(function () {
       $.mobile.changePage('#dialog');
      }, 100); // delay above zero
     }
    });
    

    This way, the dialog will popup on pageshow event and only once.

    update

    I found this interesting jQueryMobile events diagram on this blog. It explains why a dialog or a popup is fired twice on the first page and not on the rest of the pages in case of multi-pages structure. It seems it fires once the page is ready into DOM and again when on pageshow. Setting a timeout prevents the dialog from firing on pageinit, and therefore skips that event until pageshow is triggered.

    enter image description here

    Image / diagram source: http://bradbroulik.blogspot.co.nz/2011/12/jquery-mobile-events-diagram.html

    0 讨论(0)
  • 2020-11-30 10:15

    Most probably is that on first page, that event is already fired when your piece of code is executed. Which explains why you get nothing only on the first page.

    About your second point, it's normal since, changePage will "change" the page to the dialog, and once you close the dialog, it will return to your previous page. Thus, the if is executed 2 times.

    My suggestion is that for the first time you enter the first page, you can re-transition to the same page just after you register the callback for the pageshow event.

    0 讨论(0)
提交回复
热议问题