Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the closing of an external window in JS? [duplicate]

I have a piece of code that opens a window with:

  $("#authorization_link").click(function() {
    win = window.open($(this).attr("href"),'width=800,height=600');
  });

Now I want to run another block when window "win" is closed. What is the event and how do I run code on its detection?

like image 231
AKWF Avatar asked Oct 22 '22 07:10

AKWF


1 Answers

You must use intervals to check when/if the window was close Here is how you'll do it:

win = window.open($(this).attr("href"),'width=800,height=600');

function checkIfWinClosed(intervalID){
    if(win.closed){
        alert('windowsClosed');
        clearInterval(intervalID);
    }
}
var interval = setInterval(function(){
    checkIfWinClosed(interval);
},1000);

And here is a working example in fiddle

Hope that helps.

like image 136
Neta Meta Avatar answered Oct 27 '22 09:10

Neta Meta