How to detect when a tab is focused or not in Chrome with Javascript?

Posted by LLer on Stack Overflow See other posts from Stack Overflow or by LLer
Published on 2010-04-27T11:20:15Z Indexed on 2010/04/27 11:23 UTC
Read the original article Hit count: 298

Filed under:
|
|

I need to know if the user is currently viewing the current tab or not in Google Chrome. I tried to use the events blur and focus binded to the window, but only the blur seems to be working.

window.addEventListener('focus', function() {
  document.title = 'focused';
});

window.addEventListener('blur', function() {
  document.title = 'not focused';
});

The focus event works weird, only sometimes. If I switch to another tab and back, focus event won't activate. But if I click on the address bar and then back on the page, it will. Or if I switch to another program and then back to Chrome it will activate if the tab is currently focused.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about google-chrome