Hbaecklund Hbaecklund - 5 months ago 9
Javascript Question

How to add a (1) text notficiation on document title

Hi I've read here about browser tab notifications

This is the code suggested to achieve an (1) on the browser tab every second.

var count = 0;
var title = document.title;

function changeTitle() {
count++;
var newTitle = '(' + count + ') ' + title;
document.title = newTitle;
}

function newUpdate() {
update = setInterval(changeTitle, 1000);
}

var docBody = document.getElementById('site-body');
docBody.onload = newUpdate;


I've tried it and do not seem to work. Can't see why.. Input?

DEMO
http://tutsplus.github.io/tab-notification/index.html

Answer

If it's like in the example, script loaded within the body tags, try this one:

var count = 0;
var title = document.title;
var update;

function changeTitle() {
    count++;
    var newTitle = '(' + count + ') ' + title;
    document.title = newTitle;
}
(function() {
    update = setInterval(changeTitle, 1000);
})();

Also in your code variable update is undeclared. And you're not using it, so try delete "update".