Capture the close event of popup window in JavaScript

While the accepted answer is correct for same origins I found a solution for cross origin popups:

var win = window.open('http://www.google.com'); 
var timer = setInterval(function() { 
    if(win.closed) {
        clearInterval(timer);
        alert('closed');
    }
}, 1000);

Source: atashbahar.com

For those considering using it.

Even Facebook is using this “hack” in their Javascript SDK.

You can verify this by having a look at their code. Just search for .closed in https://connect.facebook.net/en_US/sdk.js.

Leave a Comment

tech