Determine if the browser tab is active? - IE?

I looked at this:

How to find out if the browser / tab is active

and

Is there a reliable way to determine if a tab or browser window is inactive or not in focus?

The first link provides a solution for modern browsers, but does not work in IE7 / 8. Both of these questions are pretty old. Is there a solution to the problem of determining whether a visitor views his open tab or not?

Pretty much everything I tried works fine in Chrome. But IE7 just fails.

I just want to set a global variable that says whether the page is being viewed.

i.e.

var isActive = true;

$(window).focus(function() {
    isActive = true;
});

$(window).blur(function() {
    isActive = false;
});

// test
setInterval(function () {
  console.log(window.isActive ? 'active' : 'inactive'); 
}, 1000);
+5
source share
2 answers

, Googling... ... ... 4 , . , , .

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

var isActive = true;
function onBlur() {
    isActive = false;
};
function onFocus(){
    isActive = true;
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}
+7

, , , , . , .

+1

All Articles