在关闭弹出窗口中打开弹出窗口并刷新父页面

时间:2012-05-29 02:23:01

标签: javascript html javascript-events

我在JavaScript中用window.open打开了一个弹出窗口,我想在关闭弹出窗口时刷新父页面。(onclose event?)我该怎么做?

window.open("foo.html","windowName", "width=200,height=200,scrollbars=no");

11 个答案:

答案 0 :(得分:211)

您可以使用“window.opener”访问父窗口,因此,在子窗口中写下以下内容:

<script>
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }
</script>

答案 1 :(得分:31)

弹出窗口没有您可以收听的任何关闭事件。

另一方面,当窗口关闭时,有一个关闭的属性设置为true。

您可以设置一个计时器来检查已关闭的属性并按以下方式执行:

var win = window.open('foo.html', 'windowName',"width=200,height=200,scrollbars=no");   
var timer = setInterval(function() {   
    if(win.closed) {  
        clearInterval(timer);  
        alert('closed');  
    }  
}, 1000); 

请参阅此working Fiddle example

答案 2 :(得分:12)

在您的子页面上,输入以下内容:

<script type="text/javascript">
    function refreshAndClose() {
        window.opener.location.reload(true);
        window.close();
    }
</script>

<body onbeforeunload="refreshAndClose();">

但作为一个优秀的UI设计,您应该使用关闭button,因为它更加用户友好。见下面的代码。

<script type="text/javascript">
    $(document).ready(function () {
        $('#btn').click(function () {
            window.opener.location.reload(true);
            window.close();
        });
    });
</script>

<input type='button' id='btn' value='Close' />

答案 3 :(得分:3)

我用这个:

<script language='javascript'>
var t;
function doLoad() {
t = setTimeout("window.close()",1000);
}

</script>
<script type="text/javascript">
function refreshAndClose() {
    window.opener.location.reload(true);
    window.close();
}
</script>
<body onbeforeunload="refreshAndClose();" onLoad='doLoad()''>

当窗口关闭时,然后刷新父窗口。

答案 4 :(得分:2)

如果打开的URL符合Same Origin Policy

window.open将返回对新创建的窗口的引用。

这应该有效:

function windowClose() {
    window.location.reload();
}

var foo = window.open("foo.html","windowName", "width=200,height=200,scrollbars=no");
foo.onbeforeunload= windowClose;​

答案 5 :(得分:1)

如果您的应用在支持HTML5的浏览器上运行。您可以使用postMessage。这里给出的例子与你的非常相似。

答案 6 :(得分:1)

在我的情况下,我点击了父页面中的链接按钮打开了一个弹出窗口。 使用

刷新关闭孩子的父母
window.opener.location.reload();
在子窗口中的

导致重新打开子窗口(可能是因为View State我猜。如果我错了,请纠正我)。 所以我决定不在父级中重新加载页面并再次加载页面,为其分配相同的URL。

为了避免在关闭弹出窗口后再次打开弹出窗口,这可能会有所帮助,

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

答案 7 :(得分:1)

试试这个

        self.opener.location.reload(); 
  

打开当前窗口的父级并重新加载该位置。

答案 8 :(得分:0)

您可以在完成所有操作的步骤之后使用父命令(父级是窗口)访问主页...

    function funcx() {
        var result = confirm('bla bla bla.!');
        if(result)
            //parent.location.assign("http://localhost:58250/Ekocc/" + document.getElementById('hdnLink').value + "");
            parent.location.assign("http://blabla.com/" + document.getElementById('hdnLink').value + "");
    } 

答案 9 :(得分:0)

您可以在父页面中使用以下代码。

<script>
    window.onunload = refreshParent;
    function refreshParent() {
      window.opener.location.reload();
    }
</script>

答案 10 :(得分:0)

以下代码将设置为关闭后刷新父窗口:

function ManageQB_PopUp() {
            $(document).ready(function () {
                window.close();
            });
            window.onunload = function () {
                var win = window.opener;
                if (!win.closed) {
                    window.opener.location.reload();
                }
            };
        }