2

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();  
    } 
}
Bazzz
  • 26,427
  • 12
  • 52
  • 69

2 Answers2

1

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

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

Oblivion2000
  • 616
  • 4
  • 9
  • Nice find. It's not really a solution but at least now I know that it's not me being crazy. :) – Bazzz Jun 08 '12 at 06:48
1

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/

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • Thanks for your suggestion, I think the `var` should be *after* the first `{`, but even then there is no difference. It remains functional in Firefox and Chrome, but produces still the same wrong behaviour in IE9. Mind to elaborate on your way of thinking here? Perhaps there is some aspect in terms of scopes that I fail to grasp. – Bazzz Dec 09 '11 at 15:14
  • Thank you for your update, I tried this in a proof of concept at home and it seemed to result in the correct behaviour, but I need this for a project at work. So next week I'll try it at work and if it solves the issue I'll accept your answer. thanks – Bazzz Dec 10 '11 at 09:21
  • This appears to work if the popup url is within the same domain as the parent window. If the popup url is in another domain, the problem remains. – Bazzz Jan 10 '12 at 07:53
  • +1 for good attempt, but as Oblivion2000 noted, it is a bug in IE9, so neither you could have provided the correct solution. – Bazzz Jun 08 '12 at 06:49
  • Thanks for the feedback. Just to note, it works for me in IE9 on Windows 7 as I just tested it :) So perhaps they fixed it along the line somewhere? IDK, I did not research it. – Mark Schultheiss Jun 08 '12 at 12:33