Javascript 关闭弹出窗口后刷新父窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11398356/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:42:43  来源:igfitidea点击:

Refreshing Parent window after closing popup

javascriptpopupwindowparent

提问by John Doe

I am creating a popup window that goes to hello.html. I want my original (parent page) to reload when i close the popup window (hello.html). I can't seem to get it to work, but I'm close. Here is the code I have so far for the main page and the hello.html page....

我正在创建一个转到 hello.html 的弹出窗口。当我关闭弹出窗口 (hello.html) 时,我希望我的原始(父页面)重新加载。我似乎无法让它工作,但我已经接近了。这是我迄今为止的主页和 hello.html 页面的代码....

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function open_win()
{
window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
}
</script>


<script language="JavaScript">

function refreshParent() {
  window.opener.location.href = window.opener.location.href;

  if (window.opener.hello.html)

 {
    window.opener.hello.html.close()
  }
  window.close();
}
</script>


</head>

<body>

<script type="text/javascript">

var d=new Date();
document.write(d);

</script>

<form>
<input type="button" value="Open Window" onclick="open_win()">
</form>
</body>
</html>

Here is the hello.html...

这是 hello.html...

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>

Hello

</body>
</html>

回答by Split Your Infinity

Subscribe to the unload eventin the child window and call the parent window from the child window to notify it is closing!

订阅子窗口中的卸载事件,从子窗口调用父窗口通知它正在关闭!

EditAdded a code sample...

编辑添加了代码示例...

function popupClosing() {
  alert('About to refresh');
  window.location.href = window.location.href;
}

var w = window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
w.onunload = function () {
  window.parent.popupClosing()
};

回答by Stoia Alex

Do it like this, after creating the popup monitor its "closed" status property in an interval. But this is added in the parent document:

在创建弹出窗口后,在一个时间间隔内监控它的“关闭”状态属性,这样做。但是这是在父文档中添加的:

var pop = window.open("page.html", "popup",
            "width=800,height=500,scrollbars=0,title='popup'");
    pop.focus();

    var monitor = setInterval(function() {

        if (pop.closed) {
            document.location.reaload()
        }

    }, 1000);

回答by Vineesh Kalarickal

Try putting this javascript code in your popup window:

尝试将此 javascript 代码放在弹出窗口中:

window.onunload = function(){
  window.opener.location.reload();
};

/onunload event will trigger when you close your popup window, and window.opener.location.reload() will reload the source (parent) window./

/ onunload 事件将在您关闭弹出窗口时触发,并且 window.opener.location.reload() 将重新加载源(父)窗口。/

回答by saun4frsh

on click event of popup close function Try...

在弹出关闭功能的点击事件尝试...

window.opener.location.reload(true);

回答by ravi shankar

This piece of code works as well if a new window is popped with the window.openfeature.

如果弹出一个带有该window.open功能的新窗口,这段代码也能正常工作。

setTimeout(function () { window.opener.location.reload(true); window.close();}, 3000);

回答by Zeke

Put this script in the header of your popup window:

将此脚本放在弹出窗口的标题中:

<script type='text/javascript'>
window.onunload = function(){
window.opener.location.reload();}
</script>

This will reload the parent window when you close the popup.

当您关闭弹出窗口时,这将重新加载父窗口。