2

I realise that you can easily check for focus using something like this:

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

However this will still be equal to false, when the user for example has the browser window open on a second screen while working on the first screen in a different application.

This is for a real-time application that reports live data, and I don't want to be long-polling when a user has the application in a tab or minimized, but I do want to run the polling when the window is on view (even if it is not focussed).

Is there a way to check whether the window is view? (that works in older browsers preferably?)

Josh Mc
  • 9,911
  • 8
  • 53
  • 66
  • afaik, some browsers naturally slow down or stop timers when the window/tab is not in focus. – Joseph May 21 '12 at 05:12
  • http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active – goat May 21 '12 at 05:54
  • Not really what I am looking for, as I don't just want 'focus' I want when it is actually in view. – Josh Mc May 21 '12 at 06:03
  • Edit: The link above has since taken in a new answer, which uses a new html5 capability, that is exactly what I wanted. – Josh Mc Dec 04 '13 at 22:20

1 Answers1

1
var hasFocus = true;
$(window)
.focus(function(eventObject){
    eventObject.stopPropagation();
    hasFocus = true;
})
.blur(function(eventObject){
     eventObject.stopPropagation();
     hasFocus = false;
 });
JackingLiu
  • 82
  • 2