【问题标题】:Refreshing Parent window after closing popup关闭弹出窗口后刷新父窗口
【发布时间】:2012-07-09 01:07:03
【问题描述】:

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

【问题讨论】:

标签: javascript popup window parent


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      尝试将此 javascript 代码放入您的弹出窗口中:

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

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

      【讨论】:

        【解决方案4】:

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

        window.opener.location.reload(true);
        

        【讨论】:

          【解决方案5】:

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

          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 将其限制为仅用户发起的操作,例如调整窗口大小和移动窗口。
          【解决方案6】:

          在子窗口订阅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()
          };
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-05-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-03
            相关资源
            最近更新 更多