Javascript 在Firefox、IE和Chrome中检测window onblur()事件?
我目前拥有以下代码:Javascript 在Firefox、IE和Chrome中检测window onblur()事件?,javascript,event-handling,cross-browser,dom-events,Javascript,Event Handling,Cross Browser,Dom Events,我目前拥有以下代码: if (document.onfocusin !== undefined) { var onfocusin = true; } else{ var onfocusin = false; } if (onfocusin === true) { document.onfocusout = ad_blur; //ad_blur is a function I defined
if (document.onfocusin !== undefined) {
var onfocusin = true;
}
else{
var onfocusin = false;
}
if (onfocusin === true)
{
document.onfocusout = ad_blur; //ad_blur is a function I defined
//when window.onblur() fires
} else{
window.onblur = ad_blur;
}
这段代码在IE 8和Chrome 17中运行良好,但在Firefox 8中不行。我还发现了一些其他代码,但兼容性不满足我的需要。所以问题是:有没有办法确定IE、Firefox和Chrome中的window.onblur()
事件
更具体地说,我希望在新窗口(或选项卡)打开时处理该事件。正如jmort253所提到的,jQuery在这方面非常出色,下面是在几乎所有当前浏览器中都可以使用的代码:
$(document).ready(function() {
$(window).blur(function() {
ad_blur(); //here is what you wanna do when blur fires
});
});
jQuery在解决这些兼容性问题方面非常出色,因此您可以专注于更大的问题。除非有非常有说服力的好理由不这样做,否则你可能想调查一下。