The code im currently using is this
var focus;
function focuswindow() { focus = true; }
function hidewindow() { focus = false; }
window.onfocus = focuswindow();
window.onblur = hidewindow();
The idea is that it can be used like this
if( focus ) { //do something
}
However its not working. Also it only needs to work on Chrome (so no legacy IE stuff) since its for a Chrome extension.
The reason it doesn’t work is that you’re calling the functions immediately and assigning the return value of
undefinedtoonfocusandonblur.Instead,
onfocusandonblurshould reference the functions by name.Try this:
Notice that I removed the
()call operators. Nowonfocusandonblurare referencing the functions, and will call them when the events occur.