good_evening
good_evening

Reputation: 21749

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?

Upvotes: 4

Views: 5568

Answers (3)

Yanick Rochon
Yanick Rochon

Reputation: 53536

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

Upvotes: 2

Saeed Neamati
Saeed Neamati

Reputation: 35822

$('title').text('your title') suffices.

To see if you're taking the right path, simply use IE's developer toolbar (F12) and go to console and write $('title'), you should see [...] in console. This means that $('title') is an object and it works up to here. Then write typeof $('title').text, and you should see function as the result. If these tests are OK, then your IE is broken.

Upvotes: 0

Josh Johnson
Josh Johnson

Reputation: 11097

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();

Upvotes: 13

Related Questions