最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cross-domain window.close event from parent window - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Mar 31, 2021 at 7:14 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 16, 2012 at 11:39 SomebodySomebody 9,64526 gold badges98 silver badges144 bronze badges 1
  • Detecting when a Cross-Domain Popup Window Closes is very similar. – Matthew Flaschen Commented Apr 19, 2012 at 14:57
Add a ment  | 

3 Answers 3

Reset to default 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 patibilty 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 munity 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 municate between cross-domain windows. For instance:

MainWindow:

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

Domain2:

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

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

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

var url = "http://stackoverflow.";
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
    ...
}

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 pile-and-go script on a cleared scope Source File: chrome://firebug/content/net/httpLib.js

    Error: attempt to run pile-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

发布评论

评论列表(0)

  1. 暂无评论