0
我試圖檢測窗口/選項卡當前是否焦點或not.My代碼工作正常,以檢測焦點失去當我切換選項卡,但它失敗,如果當前窗口被某些應用程序(音樂播放器或任何其他軟件)。我知道重點檢查可以用很多方式完成,但我有興趣有一個基於我的代碼的解決方案。可以有人告訴我爲什麼下面的代碼不能按預期工作?爲什麼onblur不能正常發射?
$(document).ready(function() {
var hidden, change, vis = {
hidden: "visibilitychange",
mozHidden: "mozvisibilitychange",
webkitHidden: "webkitvisibilitychange",
msHidden: "msvisibilitychange",
oHidden: "ovisibilitychange" /* not currently supported */
};
for (hidden in vis) {
if (vis.hasOwnProperty(hidden) && hidden in document) {
change = vis[hidden];
break;
}
}
if (change)
document.addEventListener(change, onchange);
else if (/*@[email protected]*/false) // IE 9 and lower
document.onfocusin = document.onfocusout = onchange
else
window.onfocus = window.onblur = onchange;
function onchange (evt) {
evt = evt || window.event;
if (evt.type == "focus" || evt.type == "focusin")
window_focus = true;
else if (evt.type == "blur" || evt.type == "focusout")
window_focus = false;
else
window_focus = this[hidden] ? false : true;
}
});