5

For example I'm on domain1:

a.click(function(){
  window.win=window.open(domain2,'name');
});

Now I'm on domain2 and I'm closing it. How will window.win know that user closed that window? Is there any event or property to check via interval?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Somebody
  • 9,316
  • 26
  • 94
  • 142
  • [Detecting when a Cross-Domain Popup Window Closes](http://stackoverflow.com/questions/4851438/detecting-when-a-cross-domain-popup-window-closes) is very similar. – Matthew Flaschen Apr 19 '12 at 14:57

3 Answers3

9

There is a property which is not part of any W3C spec. It's called closed and would get accessed like

if( window.win.closed ) {
    // window was closed
}

I'm not sure about the cross-browser compatibilty for that property. I'm also not sure how this behaves on cross-origin domains. But if you try it please let me and the rest of this community know about it.


Another option is that you take care for the notification yourself. That means, you are listening for the onbeforeunload within the popup-window. When the event fires, you could use HTML5's postMessage method to communicate between cross-domain windows. For instance:

MainWindow:

window.addEventListener('message', function(e) {
    if( e.origin === 'http://www.popupdomain.com' ) {
        if( e.data === 'closed' ) {
            alert('popup window was closed');
        }
    }
}, false);

Domain2:

window.onbeforeunload = function() {
    window.opener.postMessage('closed', 'http://www.popupdomain.com');
};

The only caveat on this solution is that it's only compatible with browser that support basic HTML5. There are other (sneaky) ways to have a cross-domain communication on old'ish browsers, but I guess that is another story.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • DOM object which is returned from window.open is empty. I wish there could be that closed property. Any other solution? – Somebody Mar 16 '12 at 11:53
  • Guess i'll have to listen to this another story. :) – Somebody Mar 16 '12 at 11:59
  • @Beck: that is because it doesn't work or you can't rely on html5 ? – jAndy Mar 16 '12 at 12:05
  • I can't, i'm developing for IE7 as well. – Somebody Mar 16 '12 at 12:06
  • This is a gem! Works perfectly on Chrome 31. And yes, @Somebody, if you `console.log()` the window returned from `window.open()` after it is closed, you get an empty instance of window. Try `console.log(window.win.closed)`, though. It returned `false` for me. I'd be very interested to see where else it works (particularly IE). – Bailey Parker Dec 24 '13 at 13:18
  • MDN indicates that `closed` property is part of HTML5 (https://developer.mozilla.org/en-US/docs/Web/API/Window/closed) – MartyIX Oct 11 '15 at 10:25
4

You can check if the cross domain was closed by using an interval check on the windows closed property.

var url = "http://stackoverflow.com";
var width = 500;
var height = 500;
var closeCheckInterval = 500; //Check every 500 ms.

var popup =  window.open(url, "_new", 'width=' + width + ', height=' + height);
popup.focus();

var closeCheck = setInterval(function () {
    try {
        (popup == null || popup.closed) && (clearInterval(closeCheck), onWindowClosed());    
    } catch (ex) { }
}, closeCheckInterval);

var onWindowClosed = function () {
    ...
    // Stuff to do after window has closed
    ...
}
Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
0

I was working on the similar problem in which a window of domain1 is opened from domain2. I needed to keep a check on when the window gets closed. I tried following :-

  1. I used window.onunload event , it didn't work because of Same Origin Policy and showed following error

    Error: attempt to run compile-and-go script on a cleared scope Source File: chrome://firebug/content/net/httpLib.js

    Error: attempt to run compile-and-go script on a cleared scope Source File: chrome://firebug/content/firefox/tabWatcher.js

  2. But I maintained an array of Window objects and applied "Window.closed" property , it works even in cross domain. :)

  3. Also you can try postMessage API or CORS

Kahini Wadhawan
  • 454
  • 1
  • 4
  • 13