Theia installation has been updated - September 10, 2011
« May 2012
S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Theia
Having trouble with Jquery's dialog('isOpen') ? « home
icon
posted on 16:43 - 02 May 2010 | posted by Lev
last modified on 16:43 - 02 May 2010 | last modified by Lev
Maybe it's just me, but I find the return value of jQuery's dialog('isOpen') very counter intuitive when operating on an element which hasn't yet even been made a dialog. I'm kinda shocked at how it actually works since jQuery is usually so smart otherwise.
 
If you take a look at the documentation (which is technically correct), it says this:
 

Returns true if the dialog is currently open.

 
Notice how they don't say anything about otherwise, because this is important.
 
If you try to perform this method on an element which hasn't yet been added to the page (perhaps added dynamically) or hasn't yet been made a dialog in jQuery, you need to be VERY CAREFUL about how you check the return value.
 
Let's suppose you have a DIV somewhere like this:
 
HTML CODE
<div id='myDialog'></div>
 
Let's also suppose you have some jQuery/JavaScript code checking to see if it is open as a dialog yet:
 
"JAVASCRIPT" CODE
if (!$('#myDialog').dialog('isOpen')) {alert('dialog is not open');}
 
Now, you might expect to see an alert pop up telling you the dialog isn't open, but you won't, because dialog('isOpen') returns an OBJECT if you are checking on an element that doesn't exist yet or isn't a workable jQuery dialog; and since an object being evaluated will always return true, the alert will never be invoked!
 
If you did explicitly already create the element in your mark-up, and if you have built it as a jQuery dialog like this:
 
"JAVASCRIPT" CODE
$('#myDialog').dialog({autoOpen: false});
if (!$('#myDialog').dialog('isOpen')) {alert('dialog is not open');}
 
... then you *will* see the alert saying the dialog isn't open.
 
In other words, the isOpen method is only going to explicitly return false when the element you are checking exists and has already been transformed into a dialog. If you check an element which hasn't been made a dialog yet (or may not yet exist) you should change your conditional statement to look like this instead:
 
"JAVASCRIPT" CODE
if ($('#myDialog').dialog('isOpen') !== true) {alert('dialog is not open');}
 
The difference here being that we are explicitly stating "if the return value of the isOpen method does not return the boolean value 'true' then do the alert" . This means an object being returned will no longer cut it, as would be the case if you are just prepending the statement with the exclamation point.
 
In my humble opinion, I think this behavior 'ought to be changed, since checking on elements which haven't been made dialogs yet will evaluate to true (unless explicitly checking for the boolean value true, as seen above). This caused me a whole hell of a lot of confusion since I wasn't just wanting to see if the dialogs were open - per say - but whether or not the elements were even dialogs. Since, as far as I am aware, there isn't a method to just determine whether the element is a dialog, it seems practical that performing this (isOpen) check should always return false unless it returns true.
 
Hopefully this helps someone else who may be in the same situation as I was. >_<
tags
post reply
untitled
quote
posted by: guest 148.81.254.66 · date: 16:34 - 04 January 2012
2hours of wondering why, and finally your site helps me. thanks!
Thanks
quote
posted by: guest 62.57.246.175 · date: 10:02 - 29 December 2011
I was wondering about this odd behaviour, thanks a lot!
untitled
quote
posted by: guest 210.212.206.25 · date: 17:15 - 09 December 2011
thanx dude it helps!!!
Bookmark item @
bookmarkbookmarkbookmarkbookmarkbookmark