admin管理员组文章数量:1323225
I am creating a pop-up window. After I am finished with the work on child (pop up) window and click close button, I need to call a JavaScript function of the parent window. How can I achieve this? I am not creating the child window myself but displaying the contents of some other URL.
I am creating a pop-up window. After I am finished with the work on child (pop up) window and click close button, I need to call a JavaScript function of the parent window. How can I achieve this? I am not creating the child window myself but displaying the contents of some other URL.
Share Improve this question edited Jan 2, 2023 at 14:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 5, 2010 at 13:49 biluriudaybiluriuday 4281 gold badge7 silver badges16 bronze badges3 Answers
Reset to default 9I don't think you can get an event, because you can't mess with the document itself when the URL is from a different domain. You can however poll and check the "closed" property of the window object:
var w = window.open("http://what.ever.", "OtherWindow");
setTimeout(function() {
if (w.closed) {
// code that you want to run when window closes
}
else
setTimeout(arguments.callee, 100);
}, 100);
You could also start an interval timer if you prefer:
var w = window.open("http://what.ever.", "OtherWindow");
var interval = setInterval(function() {
if (w.closed) {
// do stuff
cancelInterval(interval);
}
}, 100);
If the child window is not originating from the same domain name as the parent window, you're locked out due to the same origin policy. This is done deliberately to prevent cross-site-scripting attacks (XSS).
Don't vote for this. It is just an improvement of Pointy's code to getting rid of arguments.callee
. Vote for Pointy.
var w = window.open("http://what.ever.", "OtherWindow");
setTimeout(function timeout() {
if (w.closed) {
// code that you want to run when window closes
}
else
setTimeout(timeout, 100);
}, 100);
本文标签: dom eventsHow to call a JavaScript function of parent window when child is closedStack Overflow
版权声明:本文标题:dom events - How to call a JavaScript function of parent window when child is closed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742084302a2419885.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论