praveenjayapal
praveenjayapal

Reputation: 38529

Checking the window's focus constantly

Here I need to check the windows focus constantly, I want to check whether the user is existing on my site or else he focus some where else. This has to be done for some particular time, after they called an event.

I have used self.focus() and `window.focus() there is not use, its always return the "native code". What I suppose to do for checking my window (site) is focused or not

Upvotes: 2

Views: 425

Answers (1)

PleaseStand
PleaseStand

Reputation: 32052

You need to trap both the focus and blur events and update a variable accordingly, starting with the fact that (hopefully) the window has the focus when it is opened.

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

Upvotes: 3

Related Questions