3

I'm able to activate the following jQuery Mobile popup:

<div data-role="popup" id="waiting1" data-overlay-theme="a" data-corners="false" data-tolerance="30,15" data-dismissible="false">
     <div class="modalAlert" id="waitingContent">
      Waiting...
     </div> 
</div>

using the jQuery command:

$(waiting1).popup('open');

but then I want to confirm programmatically that the popup opened, and trigger an alert using an IF statement if it didn't. I tried using the CSS display attribute:

if ( $(waiting1).css('display') != 'block') {
    alert(
        "Error: Waiting popup should not be visible."
    );
    return( -1 );
};

...but as a jQuery Mobile popup, apparently the attribute is always "block" whether it's visible or not. What's the proper way to check this within the IF statement? Thanks for any help.

Bharat
  • 2,441
  • 3
  • 24
  • 36
Pablo Carson
  • 342
  • 1
  • 4
  • 13

4 Answers4

8

In jQuery Mobile, a class gets applied to the popup's container when it appears. ui-popup-active when it's visible, ui-popup-hidden when it's hidden. Therefore instead of checking for 'block' or ':visible', you could check for the that class:

if ( $(waiting1).parent().hasClass('ui-popup-hidden')) {
    alert(
        "Error: Waiting popup should not be visible."
    );
    return( -1 );
};
Akurn
  • 392
  • 1
  • 5
  • That was fast! Just to be clear, the error for `ui-popup-hidden` would warn that the popup SHOULD be visible. But this was the definitely the answer I was looking for, and I spent at least an hour looking, and it worked, so thanks very much! – Pablo Carson Apr 08 '14 at 04:37
  • Seems not to work in iOS' Safari but works in Chrome and Firefox. – Jonny Nov 07 '18 at 19:56
3

We can use the jQuery Mobile Popup mutex:

if ($.mobile.popup.active && $.mobile.popup.active.element[0] === $(waiting1)[0]) {
alert('popup is opened');
}
Michael
  • 31
  • 2
1

Suppose popup has the class popupLogin

if ($.mobile.activePage.find(".popupLogin").parent().hasClass("ui-popup-active")){
     alert('popup is open');
}

See this jsfiddle: http://jsfiddle.net/umerqureshi/fuy4Lz5z/

robnick
  • 1,720
  • 17
  • 27
Umer Qureshi
  • 1,736
  • 2
  • 20
  • 22
  • this answer is better, but please note before any popup transition (like fade, slide..) was done, the 'ui-popup-active' will not appeared! if u check a poup right after popup("open"), in this case Michael method still work or use 'none' transition – ob.yann May 06 '16 at 10:00
0

This worked for me:

if(!$.mobile.activePage.find(popupID).is(":visible")) $(popupID).popup('close');
Asha Antony
  • 441
  • 5
  • 8