Search code examples
javascriptinternet-explorerwindow.opener

Check for parent window using window.opener not working in IE


I have a client-side Javascript function similar to this:

function LoadParent() {
   var objParent = window.opener;
   if (objParent) {
      objParent.location.reload();
   }
   self.close();
}

Now, here's my scenario: this code is in a page that I'll call CHILD.aspx. This page is opened in a new window from what I'll call PARENT.aspx.

Here's my problem: this needs to run even if PARENT.aspx is closed (in other words, CHILD.aspx is orphaned). In this scenario, it skips over the IF statement (because it doesn't find PARENT.aspx) and just closes the window.

It works in all browsers . . . EXCEPT INTERNET EXPLODER. I am testing in IE10.

I added an alert(objParent); line after the var objParent declaration to see what was going on. I found that when this scenario came up in Firefox, it returned NULL (as expected). But when I ran it in IE, it returned an object (NOT expected). So it blows right through the IF stop sign and tries to reload the parent -- which, of course, blows up because the parent is no longer there.

Anyone have any ideas? This is a problem that has me VERY frustrated.

Note: opening CHILD.aspx as modal is NOT an option. PARENT.aspx needs to be accessible even after CHILD.aspx is opened.


Solution

  • Here's what I finally ended up doing:

    function LoadParent() {
       var objParent = window.opener;
       if (objParent) {
          if (!objParent.closed) { // this is to keep IE happy!!!
             objParent.location.reload();
          }
       }
       self.close();
    }