admin管理员组

文章数量:1245634

I have live chat on my page. I want to change the title (with something moving like in omegle) 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?

I have live chat on my page. I want to change the title (with something moving like in omegle.) 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?

Share Improve this question edited Jul 6, 2011 at 18:52 Josh Mein 28.6k15 gold badges78 silver badges88 bronze badges asked Jul 6, 2011 at 18:24 good_eveninggood_evening 21.8k69 gold badges198 silver badges306 bronze badges 9
  • 1 Possible duplicate of stackoverflow./questions/1500554/… – George Cummins Commented Jul 6, 2011 at 18:26
  • 1 possible duplicate of jQuery: how to change title of document during .ready() ? – Neil Knight Commented Jul 6, 2011 at 18:29
  • 1 @Wesley Murch: it won't be the same! You don't read my question. How can I determine if person is in the tab where the chat is or isn't – good_evening Commented Jul 6, 2011 at 18:31
  • 1 @hey: That is a separate question that would obviously require us to understand/see your code. You didn't specify that this was the issue, only the changing of the title. Obviously I am not the only one here who understood it this way. You question does not ask: "How can I determine if person is in the tab where the chat is or isn't". I suggest you add it to the question to avoid unhelpful answers then. You are also asking in the ments how to "make it move", which is not in your question. -1 for no attempt at clarity in your post. – No Results Found Commented Jul 6, 2011 at 18:33
  • 1 @Wesley Murch He did specify that was a problem. Read the second sentence in his question again. – Josh Mein Commented Jul 6, 2011 at 18:47
 |  Show 4 more ments

3 Answers 3

Reset to default 13

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

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

$('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.

本文标签: javascriptjQuery change page39s title when user in a different tabStack Overflow