How to determine if a browser window is activated

Currently using im code

var focus;
function focuswindow() { focus = true; }
function hidewindow() { focus = false; }
window.onfocus = focuswindow();
window.onblur = hidewindow();

The idea is that it can be used as

if( focus ) { //do something
}

However its not working. Also, it should only work in Chrome (so no IE obsolete things), since it is intended to be a Chrome extension.

+3
source share
1 answer

The reason it doesn't work is because you call the functions immediately and assign the return value undefinedto onfocusand onblur.

Instead, onfocusthey onblurmust reference functions by name.

Try the following:

window.onfocus = focuswindow;
window.onblur = hidewindow;

, (). onfocus onblur .

+3

Source: https://habr.com/ru/post/1767578/


All Articles