BUG: window.closed property returns incorrect values (241109)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 4.0
  • Microsoft Internet Explorer (Programming) 4.01
  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Internet Explorer (Programming) 6.0

This article was previously published under Q241109

SYMPTOMS

The window.closed property is used when a parent window opens a child window using a window.open call and the parent or child needs to know when the child or parent window, respectively, has been closed.

The opener property refers to the window that opened the current one (using window.open method call). When the child window checks the opener.closed property, it is false when the parent is open and true otherwise; that is, the value returned is reversed.

When the parent checks the status of the child window using the child window's closed property, the same incorrect results are returned.

RESOLUTION

The workaround for the child is to have the parent set a variable in the child window that maintains the correct status of the parent window. The child then uses this variable instead of the opener.closed property.

The workaround for the parent is to negate the child window's closed property. It is safer and recommended to use the same logic as for the child window to ensure that the logic does not break when this behavior is fixed in a later version of Internet Explorer

The following HTML code demonstrates the bug and also how to work around the problem.
  1. Save the following file as Parent.htm:
    <html>
    <head>
    <script>
    var childWin;
    var childOpen = false;
    </script>
    </head>
    <body BGCOLOR="white">
    <input type="button" value="Open Child" id="button1" name="button1" onclick="OpenChild()" >
    <p>
    <input type="button" value="Is Child Open(Bug)?" id="button2" name="button2" onclick="alert(childWin.closed)">
    <p>
    <input type="button" value="Is Child Open(Fix)?" id="button3" name="button3" onclick="alert(childOpen)">
    <script>
    function OpenChild()
    {
    	childWin = window.open ("Child.htm")
    	childOpen = true;
    }
    function UpdateChild()
    {
    	//Only if child window is still open, set the parentOpen property
    	if (childOpen == true)
    	{
    		childWin.parentOpen = false
    	}
    }
    window.onunload = UpdateChild;
    </script>
    </body>
    </html>
    					
  2. Save the following file as Child.htm:
    <html>
    <head>
    <script>
    var parentOpen = true
    </script>
    </head>
    <body BGCOLOR="white">
    <input type="button" value="Is Parent Open(Bug)?" id="button1" name="button1" onclick="alert(window.opener.closed)">
    <input type="button" value="Is Parent Open?" id="button2" name="button2" onclick="alert(parentOpen)">
    
    <script>
    window.onunload = UpdateParent;
    function UpdateParent()
    {
    	//Only if the parent is open, update the status of the child window
    	if (parentOpen)
    	{
    		window.opener.childOpen = false;
    	}
    }
    </script>
    </body>
    </html>
    					
  3. Navigate to Parent.htm and click the Open Child button to open the child window.
  4. Click the Is Child Open buttons before and after closing the child window to reproduce the bug and test the fix. Alternately, click on the Is Parent Open buttons before and after closing the parent window for testing.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

REFERENCES

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites: (c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Kusuma Vellanki, Microsoft Corporation.


Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbbug kbDHTML kbdocfix kbpending KB241109