Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change page's title when user in a different tab

I have live chat on my page. I want to change the title (with something moving like in omegle.com) when a new message is received and the user is not in the same tab as the live chat. When the user returns to the tab, the title would return to normal.

I guess it should be done by jQuery. Do you know any plugins or how can I do that?

like image 596
good_evening Avatar asked Dec 07 '22 20:12

good_evening


2 Answers

Title can only be edited like so:

document.title = "blah";

So you could do:

var origTitle = document.title;
document.title = "You have ("+x+") new messages - "+origTitle;

To make it flash you would have to do something with setTimeout();

var origTitle = document.title;
var isChatTab = false; // Set to true/false by separate DOM event.
var animStep = true;
var animateTitle = function() {
    if (isChatTab) {
        if (animStep) {
            document.title = "You have ("+x+") new messages - "+origTitle;
        } else {
            document.title = origTitle;
        }
        animStep = !animStep;
    } else {
            document.title = origTitle;
            animStep = false;
    }
    setTimeout(animateTitle, 5000);
};

animateTitle();
like image 131
Josh Johnson Avatar answered Dec 10 '22 13:12

Josh Johnson


try

$('title').text("some text");

Update

Apparantly, in IE, $('title')[0].innerHTML returns the content of the <title> tag, but you can't set it's value, except using document.title. I guess this should be an improvement to the jQuery API, since $('title')[0] does return a DOMElement (nodeType = 1)...

like image 32
Yanick Rochon Avatar answered Dec 10 '22 13:12

Yanick Rochon