2016-01-13 137 views
2

我想檢測瀏覽器窗口是否關注它(已選中)。我使用下面的代碼可以這樣做:JQuery Focus/Blur和非Jquery Focus/blur在IE 11(Internet Explorer 11)上不起作用

$(window).focus(function() { 
    window.focusFlag = true; 
}).blur(function() { 
    window.focusFlag = false; 
}); 

來源: Using JQuery to bind "focus" and "blur" functions for "window", doesn't work in IE

它工作在Mozilla Firefox 43.0.4,但它不能在IE 11的工作

我也嘗試過不涉及JQuery的焦點/模糊方法。

function onBlur() { 
    document.body.className = 'blurred'; 
}; 
function onFocus(){ 
    document.body.className = 'focused'; 
}; 

if (/*@[email protected]*/false) { // check for Internet Explorer 
    document.onfocusin = onFocus; 
    document.onfocusout = onBlur; 
} else { 
    window.onfocus = onFocus; 
    window.onblur = onBlur; 
} 

它也可以在Mozilla Firefox 43.0.4,但它不能在IE 11的工作

Srource: http://www.thefutureoftheweb.com/blog/detect-browser-window-focus

我能做些什麼IE 11?

+1

https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API – epascarello

+0

@epascarello的原因我使用這些過時方法是我需要支持較舊的瀏覽器版本。舊版瀏覽器不支持該API:( – Benas

+1

因此,您將其用於支持它的瀏覽器... https://github.com/ai/visibilityjs – epascarello

回答