javascript: How Can a parent window know that its child window closed?

依然范特西╮ 提交于 2019-12-18 07:42:28

问题


I want to execute a function when an opened child window is closed. Child window opening code is:

outWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=400, top=100, left=100);

i want to call a javascript function written in parent window.I am using YUI-2 for developing the plugin. How can i make it work? what event should be registered?

thanks in advance.


回答1:


You could do something like this.

var intervalID, childWindow;

childWindow = window.open("http://www.google.com");

function checkWindow() {
    if (childWindow && childWindow.closed) {
        window.clearInterval(intervalID);
        alert('closed');
    }
}
var intervalID = window.setInterval(checkWindow, 500);

References: window.setInterval and this answer.

Simple example on jsfiddle.




回答2:


You can try acces the parent window by:

window.opener.functionThatYouWant();

This code is inside de child window.

But if you open an window that the URL is in another domain (not localhost), you can't access it because of security issues.

I used this code on Firefox, I am not sure if it works crossbrowser.



来源:https://stackoverflow.com/questions/5240428/javascript-how-can-a-parent-window-know-that-its-child-window-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!