javascript - 如果子窗口已关闭,则重新加载父窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15425463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript - reload parent window if child window was closed
提问by user007
I am trying to reload a parent window if child window get closed.
如果子窗口关闭,我正在尝试重新加载父窗口。
Here is my current code which opens a popup window, but it has not programmed for refresh parent window when it get closed. I dont know what code to add
这是我当前的代码,它打开一个弹出窗口,但它没有编程为关闭时刷新父窗口。我不知道要添加什么代码
jQuery(document).ready(function($) {
jQuery('.top-buttons a').live('click', function(e){
e.preventDefault();
newwindow=window.open($(this).attr('href'),'','height=500,width=850');
if (window.focus) {newwindow.focus()}
return false;
});
});
回答by darshanags
Try:
尝试:
newwindow = window.open($(this).attr('href'), '', 'height=500,width=850');
newwindow.onbeforeunload = function () {
console.log("popup closed")
}
回答by basarat
You can access the parent window from the child window using window.opener : example
您可以使用 window.opener 从子窗口访问父窗口:示例
So to reload the parent window just call window.opener.location.reload() documentationwhen you handle the close event.
因此,要重新加载父窗口,只需在处理关闭事件时调用 window.opener.location.reload()文档即可。
回答by Samy
Try the below code on close event of the child window
在子窗口的关闭事件上尝试以下代码
window.opener.location.reload();
回答by web2008
You can use window.opener to access parent window and window.reload() to reload it when child window is closed.
您可以使用 window.opener 访问父窗口并使用 window.reload() 在子窗口关闭时重新加载它。
Please refer below URLS:
请参考以下网址:
how to access parent window object using jquery?
javascript: How Can a parent window know that its child window closed?
回答by Dakait
in the parent window define an unload event like
在父窗口中定义一个卸载事件,如
function doStuffOnUnload() {
alert("Unloaded!");
}
if (typeof win.attachEvent != "undefined") {
newwindow.attachEvent("onunload", doStuffOnUnload);
}
else if (typeof win.addEventListener != "undefined") {
newwindow.addEventListener("unload", doStuffOnUnload, false);
}
from herei took it from
我从这里拿的
回答by hammus
you need window.opener
你需要 window.opener
window.opener.location.href = window.opener.location.href;
should be added to the closing event of the window.
应该添加到窗口的关闭事件中。