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

JavascriptGoogle ChromeFocus

Javascript Problem Overview


I need to know if the user is currently viewing a 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 correctly.

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.

Javascript Solutions


Solution 1 - Javascript

2015 update: The new HTML5 way with visibility API (taken from Blowsie's comment):

document.addEventListener('visibilitychange', function(){
    document.title = document.hidden; // change tab text for demo
})

The code the original poster gives (in the question) now works, as of 2011:

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

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

edit: As of a few months later in Chrome 14, this will still work, but the user must have interacted with the page by clicking anywhere in the window at least once. Merely scrolling and such is insufficient to make this work. Doing window.focus() does not make this work automatically either. If anyone knows of a workaround, please mention.

Solution 2 - Javascript

The selected answer for the question Is there a way to detect if a browser window is not currently active? should work. It utilizes the Page Visibility API drafted by the W3C on 2011-06-02.

Solution 3 - Javascript

It might work after all, i got curious and wrote this code:

...
setInterval ( updateSize, 500 );
function updateSize(){
  if(window.outerHeight == window.innerHeight){
    document.title = 'not focused';				
  } else {
    document.title = 'focused';
  }

  document.getElementById("arthur").innerHTML = window.outerHeight + " - " + window.innerHeight;
}
...
<div id="arthur">
  dent
</div>

This code does precisly what you want, but on an ugly way. The thing is, Chrome seems to ignore the title change from time to time (when switching to the tab and holding the mouse down for 1 sec seems to always create this effect).

You will get different values on your screen, yet your title won't change.

conclusion: Whatever you are doing, don't trust the result when testing it!

Solution 4 - Javascript

For anyone who wants to swap page titles on blur and then go back to the original page title on focus:

// Swapping page titles on blur
var originalPageTitle = document.title;

window.addEventListener('blur', function(){
    document.title = 'Don\'t forget to read this...';
});

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

Solution 5 - Javascript

I found that adding onblur= and onfocus= events to inline bypassed the issue:

Solution 6 - Javascript

This could work with JQuery

$(function() {
    $(window).focus(function() {
        console.log('Focus');
    });

    $(window).blur(function() {
        console.log('Blur');
    });
});

Solution 7 - Javascript

In chrome you can run a background script with a timeout of less than 1 second, and when the tab does not have focus chrome will only run it every second. Example;

This doesn't work in Firefox or Opera. Don't know about other browsers, but I doubt it works there too.

var currentDate = new Date();
var a = currentDate.getTime();

function test() {
   	var currentDate = new Date();
	var b = currentDate.getTime();
var c =	b - a;
	if (c > 900) {
        //Tab does not have focus.
    } else {
        //It does
    }
	a = b;
	setTimeout("test()",800);
}



setTimeout("test()",1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfentView Question on Stackoverflow
Solution 1 - JavascriptninjageckoView Answer on Stackoverflow
Solution 2 - JavascriptthirdenderView Answer on Stackoverflow
Solution 3 - JavascriptBastiaan LindersView Answer on Stackoverflow
Solution 4 - JavascriptstacighView Answer on Stackoverflow
Solution 5 - JavascriptTrass VasstonView Answer on Stackoverflow
Solution 6 - JavascriptkongarajuView Answer on Stackoverflow
Solution 7 - JavascriptBen VailView Answer on Stackoverflow