问题描述
假设我有一个打开window.open()弹出窗口的按钮:
Let's say I have a button that opens a popup with window.open():
<button id = "my-button">Open window</button>
我想在此窗口关闭时捕获.我使用以下代码:
And I want to capture when this window closes. I use the following code:
$('#my-button').on('click', function() {
popup = window.open('http://www.google.com', 'my_popup', "width=800,height=600,scrollbars=no,resizable=yes,status=yes,toolbar=no,location=no,menubar=no");
$(popup.document).ready(function() {
console.log('ready!');
$(popup).on('unload', function() {
console.log('unloaded!');
} );
})
});
现在,我注意到打开的窗口暂时加载了"about:blank",然后加载了所请求的URL.那时,将触发onunload事件. 其他帖子说这是正常现象,但是我似乎没有收到第二次onunload事件(至少使用铬).
Now, I've noticed that the opened window briefly loads "about:blank" and then loads the requested url. At that time, the onunload event is fired. Other posts say that this is normal behavior, but I don't seem to get a 2nd onunload event (using chromium at least).
有人可以提出建议吗?
请参阅小提琴: http://jsfiddle.net/periklis/NJz6w/
一如既往,在此先感谢
推荐答案
好吧,我终于找到了解决方案:
ok I found a solution after all:
$('#my-button').on('click', function() {
popup = window.open('http://www.google.com', 'my_popup', "width=800,height=600,scrollbars=no,resizable=yes,status=yes,toolbar=no,location=no,menubar=no");
$(popup.document).ready(function() {
console.log('ready!');
$(popup).on('unload', function() {
console.log('unloaded!');
$(popup).on('unload', function() {
console.log('unloaded 2nd time!');
} );
} );
})
});
这在我的环境中很奇怪,但不适用于我发布的jsfiddle示例.无论如何.
Which strangely works on my environment but not for the jsfiddle example I posted. Anyway.
这篇关于javascript/jquery open.window弹出窗口在about:blank卸载后触发onunload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!