Javascript 检查弹出窗口是否关闭

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

Check if a popup window is closed

javascriptasp.netinternet-explorer-9

提问by S M Kamran

I am opening a popup window with

我正在打开一个弹出窗口

var popup = window.open('...', '...');

This javascript is defined in a control. This control is then used from a web page. I want to reload the page which opens this popup when the popup is closed.

这个 javascript 是在一个控件中定义的。然后从网页使用此控件。我想重新加载在弹出窗口关闭时打开此弹出窗口的页面。

Basically user is required to input some denominations in the popup window and submit. These denominations are then stored in user sessions. And when user clicks submit I am closing the popup window and at the same time want to refresh the window which opens this popup to refetch the updates which user made in the popup.

基本上用户需要在弹出窗口中输入一些面额并提交。然后将这些面额存储在用户会话中。当用户点击提交时,我正在关闭弹出窗口,同时想要刷新打开此弹出窗口的窗口以重新获取用户在弹出窗口中所做的更新。

I am trying to do

我正在尝试做

var popup = window.open('...','...');
if (popup) {
  popup.onClose = function () { popup.opener.location.reload(); }
}

I guess I am doing it wrong coz this isn't seems to be working.

我想我做错了,因为这似乎不起作用。

For testing the issue I've even tried this but no alert appeared.

为了测试这个问题,我什至尝试过这个,但没有出现警报。

if (popup) {
  popup.onclose = function() { 
    alert("1.InsideHandler");
    if (opener && !opener.closed) { 
      alert("2.Executed.");
      opener.location.reload(true); 
    } else { 
      alert("3.NotExecuted.");
    }
  }
}

采纳答案by mplungjan

Here's what I suggest.

这是我的建议。

in the popup you should have:

在弹出窗口中你应该有:

<script type="text/javascript">

function reloadOpener() {
  if (top.opener && !top.opener.closed) {
    try {
      opener.location.reload(1); 
    }
    catch(e) {
    }
    window.close();
  }
}
window.onunload=function() {
  reloadOpener();
}
</script>
<form action="..." target="hiddenFrame">
</form>
<iframe style="width:10px; height:10px; display:none" name="hiddenFrame" src="about:blank"></iframe>

then in the server process return

然后在服务器进程中返回

<script>
   top.close();
</script>


Old suggestions

旧建议

There is no variable called popup in the popup window. try

弹出窗口中没有名为 popup 的变量。尝试

var popup = window.open('...','...');
if (popup) {
  popup.onclose = function () { opener.location.reload(); }
}

or with a test:

或通过测试:

popup.onclose = function () { if (opener && !opener.closed) opener.location.reload(); }

PS: oncloseis not supported by all browsers

PS:并非所有浏览器都支持onclose

PPS: location.reload takes a boolean, add true if you want to not load from cache as in opener.location.reload(1);

PPS:location.reload 需要一个布尔值,如果你不想从缓存加载,请添加 true opener.location.reload(1);

回答by Amandeep

Try this:

尝试这个:

window.opener.location.reload(true);

or

或者

Into the popup before closing

关闭前进入弹窗

window.opener.location.href = window.opener.location.href;
window.close();

回答by Jcvv

Ok so what you do is as follows.

好的,你要做的如下。

create a method "setWindowObjectInParent" then call that in the popup. var popupwindow;

创建一个方法“setWindowObjectInParent”,然后在弹出窗口中调用它。变量弹出窗口;

function setWindowObjectInParent(obj)
{
popupwindow = obj;
}

Now you have an object that you can call focus on.

现在您有了一个可以调用焦点的对象。

So in the popup add

所以在弹出窗口中添加

$(window).unload(function(){
window.opener.setWindowObjectInParent();
});
window.opener.setWindowObjectInParent(window);

This will unset the obj when the popup is closed and set the object when it is opened.

这将在弹出窗口关闭时取消设置 obj 并在打开时设置对象。

Thus you can check if your popup is defined and then call focus.

因此,您可以检查您的弹出窗口是否已定义,然后调用焦点。

回答by Ash Burlaczenko

Instead you should use a modal dialog like so

相反,您应该使用像这样的模态对话框

var popup = window.showModalDialog("page.html", "", params);

This would stop your at this line until the dialog is closed.

这将在此行停止,直到对话框关闭。

Somewhere in your page.html you would then code some javascript to set the window.returnValuewhich is what will be place the the popup variable.

在 page.html 的某处,您将编写一些 javascript 来设置window.returnValue将放置弹出变量的内容。

Update

更新

Window.showModalDialog() is now obsolete.

Window.showModalDialog()现在已过时