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 undefined
to onfocus
and onblur
.
Instead, onfocus
and onblur
should reference the functions by name.
Try this:
window.onfocus = focuswindow;
window.onblur = hidewindow;
Notice that I removed the ()
call operators. Now onfocus
and onblur
are referencing the functions, and will call them when the events occur.