2014-10-12 190 views
3

我有一个打开多个窗口的Web应用程序。我的问题是当父窗口关闭/刷新时,子窗口保持打开状态。 我试过使用onunloadonbeforeunload,但它们都没有捕捉窗口关闭事件(在Chrome和Firefox中)。 我有一个窗口的数组,但刷新后,对它们的引用丢失。父窗口关闭时关闭所有子窗口

有什么其他的方式来抓住这个事件吗?

这是我的代码与关窗(运行closeAll()unloadonbeforeunload关闭我所有打开的窗口,但不刷新页面时):

window.unload = function() { 
    closeAll(); 
} 
window.onbeforeunload = function() { 
    closeAll(); 
} 

var closePopup = function(popup) { 
    removePopup(popup); 
    popup.close(); 
}; 

var closeAll = function() { 
    for (var popup in _this.popups) { 
     closePopup(_this.popups[popup]); 
    } 
} 

这仅适用于Chrome,但不能在Firefox和IE(最新版本)。

+1

*“我用onunload的和onbeforeunload试过,但他们没有抓住窗口关闭事件(在Chrome和Firefox)” *'onbeforeunload'绝对不会在Firefox和Chrome的工作。 – 2014-10-12 12:13:37

+0

“*其中没有一个[发现]窗口关闭事件*”这一事实表明您的代码中存在错误,因为正如T.J.克劳德指出,他们在那些命名的浏览器中工作。你可以向我们展示你的尝试,那么我们可以帮忙。 – 2014-10-12 12:15:08

+0

似乎这个代码适用于Chrome,但不适用于Firefox和IE。测试了这些事件之外的'closeAll'函数,它起作用。我也发现这里有多个参考,它不适用于Firefox和其他浏览器http://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in -firefox-safari-o http://stackoverflow.com/questions/20773306/mozilla-firefox-not-working-with-window-onbeforeunload – flaviu 2014-10-12 12:52:58

回答

1

找到了适当的溶液(jQuery的),在浏览器,Firefox和IE的最新的版本的工作原理。

最初想使用jQuery $().unload()函数(http://api.jquery.com/unload/),但自1.8(http://bugs.jquery.com/ticket/11733)开始不推荐使用。由于$().unload()$().bind('unload', fn)的快捷方式,我尝试使用基本的一个,它工作。

$(window).on('unload', function() { 
    closeAll(); 
}); 
+0

但它只关闭父窗口,子窗口没有关闭 – clarifier 2016-01-06 13:30:19

+1

@clarifier这可能是因为closeAll()函数需要在所有弹出窗口对象实例中运行一个循环,并在它们上调用close()。 – Sawtaytoes 2016-04-13 19:24:59

2

使用本

var popup = window.open("popup.html", "popup", "width=200,height=200"); 

window.onunload = function() { 
    if (popup && !popup.closed) { 
     popup.close(); 
    } 
}; 
+0

我有一个窗口数组,我称之为closeAll()函数,我认为我的代码与您的解决方案类似,但它仅适用于Chrome。发现有关Chrome浏览器以外的回应http://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in-firefox-safari-o所以我不知道如果您的解决方案是跨浏览器的。 – flaviu 2014-10-12 12:47:22

1

如果你打开所有的子窗口window.open,包括此javascript中的所有页面,然后CLOSEALL(假);将关闭所有子页面,孙子页面,grand-grand ...等等,并将第一个(根)页面重定向到login.aspx,并且不会干扰任何事件触发器,因为它为初始处理程序奠定了基础。

function CloseAll(bTopFound) 
{ 
    if (!bTopFound && nParent != null && !nParent.closed) { 
     //parent window was not closed 
     nParent.CloseAll(false); 
     return; 
    } else { 
     if (nParent == null || nParent.closed) 
     { 
      top.location = '/login.aspx'; 
     } 
    } 

    for (var i = 0; i < Windows.length; i++) 
    { 
     if (!Windows[i].closed) { 
      Windows[i].CloseAll(true); 
     } 
    } 
    nParent = null; 
    setTimeout(window.close, 150); 
} 

var Windows = []; 
//override window.open to inject store child window objects 
window.open = function (open) { 
    return function (url, name, features) { 
     // set name if missing here 
     name = name || "default_window_name"; 
     var newWindow = open.call(window, url, name, features); 
     Windows.push(newWindow); 
     return newWindow; 
    }; 
}(window.open); 

var nParent = null; 
window.onload = function (load) { 
    return function (e) { 
     nParent = window.opener; 
     if (load != null) { 
      load.call(e); 
     } 
    } 
}(window.onload); 

window.onunload = function (unload) { 
    return function (e) { 
     //promote first branch page to parent 
     if (nParent != null && !nParent.closed && Windows.length > 0) { 
      nParent.Windows.push(Windows[0]); 
      Windows[0].nParent = nParent; 
     } 
     //make first child window new root 
     for (var i = 1; i < Windows.length; i++) { 
      Windows[i].nParent = Windows[0]; 
      Windows[0].Windows.push(Windows[i]); 
     } 
     if (unload != null) { 
      unload.call(e); 
     } 
    } 
}(window.onunload);