Javascript 弹出窗口关闭时刷新父窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4267830/
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
Refresh parent window when the pop-up window is closed
提问by Kay
Is there any way I can refresh the parent window when a popup window is closed without adding any javascript code to the popup window?
有什么方法可以在弹出窗口关闭时刷新父窗口而不向弹出窗口添加任何 javascript 代码?
I have a page parent.php on which users can click "open popup" to open a popup window. This popup window shows some flash content and its not possible for me to add something like
我有一个页面 parent.php,用户可以在该页面上单击“打开弹出窗口”来打开一个弹出窗口。此弹出窗口显示了一些 Flash 内容,我无法添加类似的内容
window.onunload = function(){
window.opener.location.reload();
};
to the popup window page markup.
到弹出窗口页面标记。
Is there any other method to achieve this? Thanks
有没有其他方法可以实现这一目标?谢谢
回答by Tim Down
To make this work in all major browsers, you need to handle the unload
event handler in the pop-up and do the reloading in the main window. In the main window, add
为了在所有主流浏览器中都能正常工作,您需要处理unload
弹出窗口中的事件处理程序并在主窗口中重新加载。在主窗口中,添加
function popUpClosed() {
window.location.reload();
}
In the pop-up:
在弹出窗口中:
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
So the answer to your question is generally no, if you need your code to work in all browsers, in particular IE.
所以你的问题的答案通常是否定的,如果你需要你的代码在所有浏览器中工作,特别是 IE。
回答by benhowdle89
I'm sure you can just add this to parent.php:
我确定您可以将其添加到 parent.php 中:
var myPop = "pop up window selector"
myPop.onunload = function(){
location.reload();
};
回答by Lenny Markus
The problem with Tim Down's method is that it doesn't answer the original question. The requirement is that you cannotadd any code to the pop-up window.
Tim Down 方法的问题在于它没有回答最初的问题。要求是您不能向弹出窗口添加任何代码。
One solution that I've found, while not particularly elegant, is effective across all browsers I've tested on.
我发现的一种解决方案虽然不是特别优雅,但在我测试过的所有浏览器中都有效。
You will be simply polling the newly created window object continuously, checking if it's still open.
您将简单地连续轮询新创建的窗口对象,检查它是否仍然打开。
On parent window:
在父窗口上:
var register;
var poll;
function isOpen(){
if(register.closed){alert("Closed!"); clearInterval(poll);}
}
function create(){
register = window.open("http://www.google.com","register","width=425,height=550");
poll=setInterval("isOpen()",100); //Poll every 100 ms.
}
回答by salexch
had similar problem to detect the closing popup in the parent window. I think the reason was in not setting the document.domain property.
有类似的问题来检测父窗口中的关闭弹出窗口。我认为原因在于没有设置 document.domain 属性。
any way add to Tim Down answer the document.domain property for both window and popup like this:
以任何方式添加到 Tim Down 回答窗口和弹出窗口的 document.domain 属性,如下所示:
<script type="text/javascript">
document.domain='<?=$_SERVER['SERVER_NAME']?>';
</script>
and instead of
而不是
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
I used in the popup :
我在弹出窗口中使用了:
<body onunload="window.opener.popUpClosed();">