dieki
dieki

Reputation: 2475

window.onFocus intermittently doesn't fire on Chrome

I'm trying to determine whether or not a tab is focused, for a chatroom app. I have:

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

This works perfectly in Firefox and even in IE. But in Chrome, it only works intermittently; sometimes the event will fire, sometimes not. It will always fire if I click on a different window then click back to the Chrome window; but switching tabs doesn't always do it.

What can I do about this?

See live example here: http://holyworlds.org/new_hw/chat/onfocus.html

Upvotes: 0

Views: 1307

Answers (2)

dieki
dieki

Reputation: 2475

This appears to be a bug in Chrome\Windows, since Chrome on other platforms is unaffected.

Bug filed here: http://code.google.com/p/chromium/issues/detail?id=87812

Upvotes: 1

mplungjan
mplungjan

Reputation: 178413

You forgot the rest of the script.

Try this

var isActive = true;
window.onfocus = function () {
  isActive = true;
    document.title = window.isActive;
};
window.onblur = function () {
  isActive = false;
    document.title = window.isActive;
};

Upvotes: 0

Related Questions