JavaScript close window when the open is closed in IE

For my web application, I need to close the child window when closing the parent window. By "closed" I mean that the browser window is actually closed, and not just moved to a new page.

I saw " How to close a child window if the parent window is closed? ", But my option is an extension. The answer to this question solves the problem of closing the child window for any parent unloading event. However, unload! = Close (IMO); just clicking on the link fires the unload event.

Since there is no "onclose" event in JS, I decided that the best method is on the parent unloaded setTimeout event on the child, to find out if its parent exists and closes if not:

var w = window.open("", "Logger", "height=480,width=640,resizeable,scrollbars=yes");
if (w) {
  JSEvents.on(window,'unload',function(){
    if (w && !w.closed) {
      w.setTimeout(function(){
        //IE this==w.opener
        if (!w.opener || w.opener.closed) {
          w.close();
        }
      },500);
    }
  });
}

, , IE (7) setTimeout unload . this == w.opener setTimeout. :

JSEvents.on(window, 'unload', function(){
  window.setTimeout(function(){alert('HERE');},500);
});

alert setTimeout .

setTimeout , ?

, , ?

FF , , IE.

+3
6

, script - DOM script. , , IE.

var w = window.open("", "Logger", "height=480,width=640,resizeable,scrollbars=yes");
if (w) {
  JSEvents.on(window,'unload',function(){
    if (w && !w.closed) {
      var srpt = w.document.createElement('script');
      srpt.type = 'text/javascript';
      srpt.text = 'window.setTimeout(function(){if(!window.opener||window.opener.closed){window.close();}},250);';
      w.document.body.appendChild(srpt);
    }
  });
}

, . , script src.

+1

setTimeout , ?

IE. IE , , , , (, - , ) . , IE , , , , , " script".

. onunload (, w.parentUnloaded = true), setInterval -

if (window.parentUnloaded && (!window.opener || window.opener.closed))

IE? ... - , . , , . , , .

(*), - , . ; div , .

(*: (**), , JavaScript . , "a" "b" , "b" . "b" , JavaScript , . poller , . postMessage HTML5, .)

(**: , , , , , , , IE Sun Java .)

, ; .

, .; -)

, , , , - . , , , "reset .

, , , !:-) , , opener.location , , . , , , try... catch, , .

bucabay ( ):

, . , .

. , , . - . (, , ), IE/FF/Op/Saf/Chr.

+5

: ( , )

 var int = window.setInterval(function(){
   // On opener domain change, all browsers throw an error. Lets use that error to our advantage using try/catch:
   try
     {
    if(opener && typeof opener.document != 'undefined')
    {     
     // Adding this variable fixes IE8. Why? Because F U thats why.
     var openerRef = window.opener.location.host;
    }
    else{
     // Loads the survey when opener is closed
     window.location = 'exit-survey.jsp';
    }
     }
   // Loads the survey if an error throws (error throws when opener changes domain)
   catch(err)
     {
    window.location = 'exit-survey.jsp';
     }
  }, 500);
})
+2

:

window.closeWithParent = function() {
  if (!window.opener || window.opener.closed) {
    window.close();
  }
};
window.parentClosing = function() {
  window.setTimeout(window.closeWithParent, 500);
};

:

JSEvents.on(window,'unload', function() { 
  if (w.parentClosing) w.parentClosing(); 
});

, , , . , , setTimeout () , ( ).

+1

- , . , .

, , , , ( ).

, , , , , .

, , , , , .

- .

+1

, . , .

, , , JavaScript. (, window.opener)

, - . , , .

cookie DOM Storage .. , cookie ( , DOM - FF2 +, IE8 + ). , .

, , , . .

:

// parent
setInterval(function() { setCookie('parent_alive', new Date()) }, 1000);
// child
setInterval(function() { if (readCookie('parent_alive') < new Date()-5000) window.close() }, 1000)

Here, the child closes 5 seconds after the parent does not refresh the cookie "parent_alive". The main problem is that an Internet connection can interfere with the page loading within 5 seconds, according to which the child believes that it was closed. So this is a balancing act.

Please note that polling is quite effective if you use session cookies, as they remain in memory. However, if you use persistent cookies, you are likely to beat a drive that sucks.

+1
source

Source: https://habr.com/ru/post/1716626/


All Articles