Link to home
Start Free TrialLog in
Avatar of jkesce
jkesce

asked on

window.opener.name - access denied

Good Morning!

I have read through every answer to any question related to window.opener and cannot find a solution to my problem.

I have a main window that opens a popup help window. One of the links in the popup help menu is supposed to load a url into the main window and then close the popup window.

The issue is this - if the main window if closed I need to open a new window with the url specified. If the main window is open everything works just fine. If the main window is closed and I try to access any property of window.opener I get an Access Denied error. The window.opener returns an object reference even if the opener has been closed. If the opener is open I can access it fine and even check the name of the window.

window.opener.closed - always returns false as others have mentioned.

Thanks for the help.

Jeannette
Avatar of jgr4
jgr4

The following works for me - I'm in win2K, IE6

<html>
<body>
<a href="javascript:OpenWindow()">Open a new window</a><br>
<a href="javascript:OpenInParent()">Open a page in the main wnd</a>

<script>
function OpenWindow()
{
     open("test.html","child");
}

function OpenInParent()
{
     if(window.opener.closed == true)
          open("test.html", "main");          
     else
          window.opener.location.replace("test.html");
}
</script>
</body>
</html>
Avatar of jkesce

ASKER

Just to clarify - I need something IE 5 and Netscape 4.5 compatible.

Consider this:

Window A opens Window B onclick of a button with javascript:window.open (blah blah)

On Window B there is a link. On click of the link I need to check the status of the opener. In this case the below function would need to be in my code for Window B. This is what does not work. I can never get a value of true from the first statement if the code is in Window B.

function OpenInParent()
{
    if(window.opener.closed == true)
         open("test.html", "main");          
    else
         window.opener.location.replace("test.html");
}


Avatar of jkesce

ASKER

Just to clarify - I need something IE 5 and Netscape 4.5 compatible.

Consider this:

Window A opens Window B onclick of a button with javascript:window.open (blah blah)

On Window B there is a link. On click of the link I need to check the status of the opener. In this case the below function would need to be in my code for Window B. This is what does not work. I can never get a value of true from the first statement if the code is in Window B.

function OpenInParent()
{
    if(window.opener.closed == true)
         open("test.html", "main");          
    else
         window.opener.location.replace("test.html");
}


Avatar of jkesce

ASKER

I was able to solve this problem with the following code:

function OpenWindow(strPage){
     if(typeof(top.opener.document) == "object") {
          top.opener.location.href=strPage;    
     }
        else{
          window.open (strPage);
     }          
     top.close();
}

If the main window is no longer open top.opener still returns an object, but an attempt to access any of the properties results in an Access Denied error. The solution is to check if the typeof function returns "object" or "unknown". If this value is "unknown" the opener has been closed.

I will request this question be deleted.

Jeannette
ASKER CERTIFIED SOLUTION
Avatar of kodiakbear
kodiakbear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
glad you got a solution. for info, the closed property should work:
function OpenWindow(strPage){
    if (!top.opener.closed) {
         top.opener.location.href=strPage;    
    }
       else{
         window.open (strPage);
    }          
    top.close();
}