Check if window has focus
- by user2536244
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.