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

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

Open popup and refresh parent page on close popup

javascripthtmljavascript-events

提问by ArMaN

I opened a popup window by window.open in JavaScript, i want to refresh parent page when I close this popup window.(onclose event?) how can I do that?

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

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

回答by Morrison Cole

You can access parent window using 'window.opener', so, write something like the following in the child window:

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

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

回答by Zuul

The pop-up window does not have any close event that you can listen to.

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

On the other hand, there is a closed property that is set to true when the window gets closed.

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

You can set a timer to check that closed property and do it like this:

您可以设置一个计时器来检查关闭的属性,并这样做:

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

See this working Fiddle example!

请参阅此工作 Fiddle 示例

回答by Ray Cheng

on your child page, put these:

在您的子页面上,放置这些:

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

and

<body onbeforeunload="refreshAndClose();">

but as a good UI design, you should use a Close buttonbecause it's more user friendly. see code below.

但是作为一个好的 UI 设计,你应该使用 Close,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' />

回答by Patrick Kershner

I use this:

我用这个:

<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()''>

when the window closes it then refreshes the parent window.

当窗口关闭时,它会刷新父窗口。

回答by cranz

window.open will return a reference to the newly created window, provided the URL opened complies with Same Origin Policy.

window.open 将返回对新创建窗口的引用,前提是打开的 URL 符合同源策略

This should work:

这应该有效:

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

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

回答by Jitendra Sawant

In my case I opened a pop up window on click on linkbutton in parent page. To refresh parent on closing child using

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

window.opener.location.reload();

in child window caused re open the child window (Might be because of View State I guess. Correct me If I m wrong). So I decided not to reload page in parent and load the the page again assigning same url to it.

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

To avoid popup opening again after closing pop up window this might help,

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

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

回答by nmkyuppie

Try this

尝试这个

        self.opener.location.reload(); 

Open the parent of a current window and reload the location.

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

回答by Chris Li

If your app runs on an HTML5 enabled browser. You can use postMessage. The example given there is quite similar to yours.

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

回答by UJS

Following code will manage to refresh parent window post close :

以下代码将设法在关闭后刷新父窗口:

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

回答by Hamit YILDIRIM

You can reach main page with parent command (parent is the window) after the step you can make everything...

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

    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 + "");
    }