/    Sign up×
Community /Pin to ProfileBookmark

check if window opener exists

To check if a parent page exist is easy

if(parent.frames.length==0)

but it seems like if opener, the similar woun’t work…

if(window.opener.length==0) gives errors

Any ideea how to check if an opener exists?

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@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

efficient as well. I'm gonna play some golf.
Copy linkTweet thisAlerts:
@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]
Copy linkTweet thisAlerts:
@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>
Copy linkTweet thisAlerts:
@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

regardless of the parents existence.
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@oleragFeb 28.2004 — Strange. When I invoke the function I last posted from a

"popup" (after the parent window is closed) it, the function,

returns the alert containing the "parent open" string for both

IE6 and NN7. But actually I believe we've already spent too

much time on this issue...
Copy linkTweet thisAlerts:
@KorauthorFeb 29.2004 — ... ok, so, please, how it remains with the genuine problem... Have you decided which is best way?
Copy linkTweet thisAlerts:
@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>

    <form action="#">
    <p>
    <input type="button" name="btn1" value="Test1 (olerag)" onClick="testParent1()">
    <input type="button" name="btn2" value="Test2 (fred1)" onClick="testParent2()">
    <input type="button" name="btn3" value="Test3 (fred2)" onClick="testParent3()">
    <input type="button" name="btn3" value="Test4 (kor)" onClick="testParent4()">
    </p>
    </form>
    [/code]
    Copy linkTweet thisAlerts:
    @KorauthorMar 01.2004 — Ok, Thank you all!
    Copy linkTweet thisAlerts:
    @WebskaterMar 01.2004 — This works for me.

    if (window.opener && window.opener.open && !window.opener.closed)
    ×

    Success!

    Help @Kor spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...