the same origin policy prevents Javascript from detecting such events. But there is a rather simple solution that allows you to detect the closing of such windows:
var openDialog = function(uri, name, options, closeCallback) {
var win = window.open(uri, name, options);
var interval = window.setInterval(function() {
try {
if (win == null || win.closed) {
window.clearInterval(interval);
closeCallback(win);
}
}
catch (e) {
}
}, 1000);
return win;
};
What it does: create a new window with the parameters provided and then set the function of the verifier with interval 1s. The function then verifies if the object of the window is present and has its property closed set to false. If either of these two is not true, this means that the window is (probably) closed and we should trigger the callback 'closeCallback function'.
This function should work with all modern browsers. Some time ago, Opera caused errors when verifying Windows properties in other domains, therefore, the try..catch block. But I've tried it now and it seems to work pretty well.
We use this technique to create 'Facebook style' login windows for sites that do not support them through the SDK (ehem ... Twitter ... ehem). This required a bit of additional work: we could not receive any Twitter messages, but Oauth redirected us to our domain, and then we could put some data in the object of the pop-up window that could be accessed from the opener. Then, in the closed callback function, we analyze that data and present the actual results.
One drawback of this method is that the callback is invoked AFTER the window has been closed. Well, this is the best I could achieve with the cross-domain policies implemented.
Source: link
I made a small example with the previous code like this:
const seCerro = () => {
alert();
}
var openDialog = function(uri, name, options) {
var win = window.open(uri, name, options);
var interval = window.setInterval(function() {
try {
if (win == null || win.closed) {
window.clearInterval(interval);
seCerro();
}
}
catch (e) {
}
}, 1000);
return win;
};
openDialog("https://www.youtube.com","Youtube","width=500,height=300");
You can see it working here