Hbaecklund
Hbaecklund

Reputation: 279

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

Upvotes: 0

Views: 69

Answers (2)

Jack jdeoel
Jack jdeoel

Reputation: 4584

don't use element.onload because it's still doesn't have (load) that Id when you are run code,check only

if(docBody) newUpdate();

Upvotes: 1

Said Hasanein
Said Hasanein

Reputation: 320

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".

Upvotes: 1

Related Questions