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

10

我正在创建一个弹出窗口,它会跳转到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>

这里是hello.html的内容:

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

Hello

</body>
</html>

请查看此链接:http://stackoverflow.com/questions/18321323/submit-form-reload-parent-and-close-child/36855748#36855748 可能会对某些人有所帮助。 - NoNaMe
7个回答

12

在子窗口中订阅unload事件,并从子窗口调用父窗口以通知其关闭!

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

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()
};

3

在创建弹出窗口后,按照以下方式操作,定期监控其“closed”状态属性。但是此操作添加到父文档中:

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);

这是一个相当糟糕的解决方案,但它是我在Chrome 55.0.2883.87中唯一有效的方法。所有其他方法都可以在onclick事件中正常工作,这让我相信Chrome将其限制为仅限于用户启动的操作,类似于调整大小和移动窗口。 - Shaun

2

尝试将以下JavaScript代码放入您的弹出窗口中:

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

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


1

在弹出窗口关闭的点击事件中,尝试执行以下函数...

window.opener.location.reload(true);

0
将此脚本放置在弹出窗口的头部:

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

这将在关闭弹出窗口时重新加载父窗口。

0

如果使用 window.open 功能弹出新窗口,这段代码同样有效。

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

0
<script type="text/javascript">
    if ($("#<%=hfldClose.ClientID%>").val() === "Close") { // create hfldClose as hidden field and assign value by backend
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        layerRefresh();
        window.parent.layerRefresh();
    }

    function layerRefresh() {
        window.parent.location.href = "your Routing url";
    }
</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接