Microsoft KB Archive/241109

= BUG: window.closed property returns incorrect values =

Article ID: 241109

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * 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.   Save the following file as Parent.htm: var childWin; var childOpen = false;     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;   Save the following file as Child.htm: var parentOpen = true   

window.onunload = UpdateParent; function UpdateParent {   //Only if the parent is open, update the status of the child window if (parentOpen) {       window.opener.childOpen = false; } }                    Navigate to Parent.htm and click the Open Child button to open the child window.</li> 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.</li></ol>

<div class="status_section">

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

<div class="references_section">