例如,我有这样的 JavaScript:
 windowHandle = window.open('http://www.irt.org/','testWindow','height=200,width=200');
我想检查“testWindow”是否已关闭并运行一个函数(如果是)。
我已经用谷歌搜索了这个问题,但到目前为止我发现的是:
if (testWindow.close) {..}
只运行一次。所以我想知道关闭窗口时是否触发了事件?谢谢。
例如,我有这样的 JavaScript:
 windowHandle = window.open('http://www.irt.org/','testWindow','height=200,width=200');
我想检查“testWindow”是否已关闭并运行一个函数(如果是)。
我已经用谷歌搜索了这个问题,但到目前为止我发现的是:
if (testWindow.close) {..}
只运行一次。所以我想知道关闭窗口时是否触发了事件?谢谢。
对于这样的任务,我会使用灯箱而不是 window.open。大多数情况下,最好将用户嵌入到您的网站中,尤其是在您进行上传之类的操作时。
我过去曾使用 colorbox 来完成类似的任务。这是一个链接http://www.jacklmoore.com/colorbox/
然后您只需将回调附加到颜色框的关闭事件
实际上,您可以在MDN 最佳实践中找到更好的实现
来自 MDN 的代码片段
var windowObjectReference = null; // global variable
function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */
  {
    windowObjectReference = window.open("http://www.spreadfirefox.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
通过这种方式,您可以可靠地确定窗口是否关闭。
希望这可以帮助
对于文件上传,您可能想尝试不可见的 iframe hack,而不是单独的窗口。或者上面 Matthew Bucci 建议的灯箱。