This is what I have so far, but it's ugly and feels non-best practicey. For example, if multiple messages are received, it starts the changeTitle cycle multiple times.
var hasFocus = true;
$(window).bind("blur", function() {
hasFocus = false;
});
$(window).bind("focus", function() {
hasFocus = true;
document.title = 'SiteName | Chat';
});
var i=0;
function changeTitle() {
i++;
if(i%2) {
document.title = 'New message on SiteName!';
}
else {
document.title = 'SiteName | Chat';
}
if(!hasFocus) {
setTimeout('changeTitle()',1000);
}
}
// Then I call changeTitle() when a new message is received.