Check if window has focus

Posted by user2536244 on Stack Overflow See other posts from Stack Overflow or by user2536244
Published on 2013-06-30T10:11:57Z Indexed on 2013/06/30 10:21 UTC
Read the original article Hit count: 209

Filed under:
|

I need to check if the user has windows on focus, I'm currently doing this:

var isonfocus=true;  
window.onblur = function(){  
  isonfocus=false;  
}  
window.onfocus = function(){  
  isonfocus=true;  
}

And whenever I need to check if the user has the windows on focus I just do if(isonfocus==true).

Problem: if the user loses focus before the page loads, even if I do if(isonfocus==true) it will return true, even though the window is not on focus, and defining the var to false var isonfocus=false; will do the reverse.

Can someone help me? Thanks.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery