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

时间:2012-07-09 15:34:39

标签: javascript popup window parent

我正在创建一个转到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>

6 个答案:

答案 0 :(得分:11)

订阅子窗口中的unload event并从子窗口调用父窗口以通知它正在关闭!

修改添加了代码示例...

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

答案 1 :(得分:3)

在创建弹出窗口监视器的间隔“关闭”状态属性后,执行此操作。但这是在父文档中添加的:

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

答案 2 :(得分:2)

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

window.onunload = function(){
  window.opener.location.reload();
};
关闭弹出窗口时会触发

/ onunload事件,window.opener.location.reload()将重新加载源(父)窗口。 /

答案 3 :(得分:1)

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

window.opener.location.reload(true);

答案 4 :(得分:0)

如果使用window.open功能弹出一个新窗口,这段代码也可以正常工作。

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

答案 5 :(得分:0)

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

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

关闭弹出窗口时会重新加载父窗口。