@oleragFeb 28.2004 — #To completely check on a parent's existence (or if it did
exist and then was closed) you can try this.. [code=php] function testParent() { if (window.opener != null && !window.opener.closed) alert("Parent open"); else { alert("Parent closed/does not exist."); } [/code]
Whoops - [b]fredmv[/b] is faster and, apparently, more
@oleragFeb 28.2004 — #Fred - I tested the code your referenced and the alert still
indicated the parent was open after I closed it. I get the
same results in both IE6 and NN7. Here's the function code;
am I missing something?? [code=php] function testParent2() { if (typeof opener != 'undefined') alert("Parent open"); else alert("closed or does not exist"); } [/code]
@fredmvFeb 28.2004 — #Actually, you are completely correct. I should've tested that code before providing it. The reason it doesn't work is simple: the [font=courier]opener[/font] object exists even if there [i]is[/i] technially no opener — but it's set to [font=courier]null[/font] thus making my [font=courier]typeof[/font] check fail. What I should've done is also checked for a property on the opener (e.g., [font=courier]opener.document[/font]). Alternatively, using the logical NOT operator ([font=courier]![/font]) would've worked just as good:<a href="#" onclick="alert(!opener ? 'closed' : 'open');">foo</a>
@oleragFeb 28.2004 — #Re-writing it another way, I'm still getting improper results
when the parent is closed... [code=php] function testParent3() { if (!opener) alert("Parent closed or does not exist"); else alert("Parent open"); } [/code]
Maybe I didn't write it correctly?!?
I've found it necessary to test both the "null" condition (if
a static popup is opened as a parent, unlikely) and to
include a test of the opener's properties since, as you
already mentioned, the actual opener object remains
@fredmvFeb 28.2004 — #The code I provided in my previous post worked perfectly; I also don't see a reason as to why yours shouldn't work correctly. Moreover, using the logical NOT operator seems to make the most sense since if [font=courier]opener[/font] is [font=courier]null[/font], the boolean statement would then evaluate to [font=courier]false[/font]. I also like checking for an object in the opener's window just to make sure, though.
@oleragFeb 29.2004 — #Have you decided which is best way?[/quote] I say #3, below, but [b]fredmv[/b] may differ. For details, see
test code sample below.
1. Your original submission always returns a "closed"
window condition, even if the parent window is up on the
desktop and previously opened the popup (test4).
2. Fred's suggestion of (!opener - test 3) seems most
logical and efficient but I had problems with it as it reported
the parent still being open after it was closed. It did test
successfully if the popup was opened directly. His earlier
test2 performed the same as test3.
3. The example I provided (test1) tested both occurances
successfully but it requires two test statements in the same
"if".
When testing, I was using IE6 and NN7 (Gecko).
[code=php] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="Content-Script-Type" content="text/javascript"> <title>Parent Test Sample</title> <script type="text/javaScript"> <!-- var closeStr = "Parent closed or does not exists."; var openStr = "Parent is currently open.";
function testParent1() { if (window.opener != null && !window.opener.closed) alert(openStr); else alert(closeStr); }
function testParent2() { if (typeof opener != 'undefined') alert(openStr); else alert(closeStr); }
function testParent3() { if (!opener) alert(closeStr); else alert(openStr); }
function testParent4() { if (parent.frames.length == 0) alert(closeStr); else alert(openStr); } --> </script>
<p style="text-align: center"> <strong>Parent Test Sample</strong> </p>