问题描述
我在寻找弹出式窗口的关闭事件。我找到一个,但我需要它用于HTML。 / p>
Popup具有关闭
属性。
>>> var popup = open('http://example.com/','popup','height = 400,width = 500');
>>>> popup.closed
false
好吧,我可以。
function open_popup(){
var popup = open('http://example.com/','popup','height = 450,width = 450');
var timer = setInterval(function(){
if(popup.closed){
alert('popup closed!');
clearInterval(timer);
}
},500);
}
我已经测试了它在Chrome 4.0.249.27,Opera 10.10,Safari 4.0 .4和Firefox 3.5.5。一切正常。
但是setInterval打扰我。这是丑陋。有更好的方法吗?
UPDATE:
我使用弹出窗口验证对话框(oAuth,实际上)。我想通过弹出窗口关闭(通过postMessage)后发送一些数据到父窗口。
页面来自另一个域的弹出窗口。因此,由于安全限制,我无法添加任何事件( unload
)。
我无法使用iframe由于iframe buster脚本。因此,我不能使用任何花哨的jQuery模态对话框。
我不能在弹出窗口中编辑任何内容。
您可能想查看 unload
事件,请查看
编辑:如您所说,您无法在弹出式窗口中编辑任何内容,实际上没有任何选项。我相信你当前的 setInterval
代码做的工作很好。你应该问自己,弹出关闭的实时检测是绝对关键。这500毫秒的计时器肯定不会几乎没有任何资源,或带来一些计算机的膝盖。
I'm looking for close event for popup. I've found one for XUL, but I need it for HTML.
Popup has closed
property.
>>> var popup = open('http://example.com/', 'popup', 'height=400,width=500');
>>> popup.closed
false
Well, I can check it once at half second.
function open_popup() {
var popup = open('http://example.com/', 'popup', 'height=450,width=450');
var timer = setInterval(function(){
if (popup.closed) {
alert('popup closed!');
clearInterval(timer);
}
}, 500);
}
I've tested it on Chrome 4.0.249.27, Opera 10.10, Safari 4.0.4, and Firefox 3.5.5. All works fine.
But setInterval bother me. It is ugly. Is there a better way of doing this?
UPDATE:I use popups for authentication dialog (oAuth, actually). I wanna send some data to parent window after popup close (through postMessage).
Page inside popup from another domain. So, I can not add any event (unload
) to it due security restrictions.
I can not use iframe due to iframe buster script. So, I can not use any fancy jQuery modal dialogs.
I can not edit anything inside popup.
You might want to look into the unload
event, take a look at Javascript: Popups
edit: as you've said you cannot edit anything inside the popup, there really aren't any options left. I believe your current setInterval
code does the job just fine. You should ask yourself if realtime detection of the popup closing is absolutely critical. That 500 milliseconds timer certainly won't strain hardly any resources or bring someones computer to its knees.
这篇关于处理弹出式关闭的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!