Bazzz
Bazzz

Reputation: 26922

window.closed IE9 reporting true when window is still open

UPDATE 2012-01-10
the popup URL is in another domain as the parent window, which appears to be the issue! How can I solve it?

I'm using the following code to detect whether the popup window is closed. It works fine in Firefox 8 and Chrome but doesn't function as expected in IE9. In IE9 the alert with "true" shows already when the popup is still open. How come IE9 has a reference to the window and the closed property reports true when the window is still open? And how can I fix it?

Javascript

var dialogWindow;
var dialogTimer;
function openDialog(url, name, options) {
 dialogWindow = window.open(url, name, options);   
 dialogTimer = setInterval(function() {   
  if(dialogWindow.closed) // IE9 reports true and executes function
  {  
   alert(dialogWindow.closed); // alert with "true"
   clearInterval(dialogTimer);  
   window.location.reload();  
  }  
 }, 2500);

 if (dialogWindow && dialogWindow.focus) 
  dialogWindow.focus();     
}

UPDATE I also tried the following approach, which shows the exact same behaviour in IE9

var dialogWindow;
var dialogTimer;
function openDialog(url, name, options)
{
    dialogWindow = window.open(url, name, options);   
    dialogTimer = setInterval("checkDialogOpen()", 2500);

    if (dialogWindow && dialogWindow.focus) 
        dialogWindow.focus();     
}

function checkDialogOpen()
{
    if(dialogWindow.closed) 
    {  
        alert(dialogWindow.closed);
        clearInterval(dialogTimer);  
        window.location.reload();  
    } 
}

Upvotes: 2

Views: 3038

Answers (2)

Oblivion2000
Oblivion2000

Reputation: 616

From what I can tell, this is a bug in IE9.

http://support.microsoft.com/kb/241109

Upvotes: 1

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

you have an issue with your script Change:

dialogTimer = setInterval(function()
      {
        if(dialogWindow.closed) // IE9 reports true and executes function 
     {

to

dialogTimer = setInterval(function()
    {
        var dialogClosedStatus = dialogWindow.closed;
        if(dialogClosedStatus) // IE9 reports true and executes function
      { 

EDIT: My typing was bogus:fixed Test page: http://jsfiddle.net/MarkSchultheiss/k2jHS/

special note: the popup will keep appearing due to your window reload in my test page example.

NOTE: if that does not do the trick, try setting the variable to null as this example: http://jsfiddle.net/MarkSchultheiss/k2jHS/2/

Upvotes: 1

Related Questions