4

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?

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
good_evening
  • 21,085
  • 65
  • 193
  • 298
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1500554/is-there-a-way-using-jquery-to-change-the-document-title-after-the-page-has-loa – George Cummins Jul 06 '11 at 18:26
  • @George Cummins: actually, no. – good_evening Jul 06 '11 at 18:27
  • 1
    possible duplicate of [jQuery: how to change title of document during .ready() ?](http://stackoverflow.com/questions/180103/jquery-how-to-change-title-of-document-during-ready) – Neil Knight Jul 06 '11 at 18:29
  • @hey - The answers will be the same to this as to the other questions. If you have tried the results from those questions, make note of it in your post and why it didn't work to avoid the close votes. – Wesley Murch Jul 06 '11 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 Jul 06 '11 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 comments how to "make it move", which is not in your question. -1 for no attempt at clarity in your post. – Wesley Murch Jul 06 '11 at 18:33
  • 1
    @Wesley Murch He did specify that was a problem. Read the second sentence in his question again. – Josh Mein Jul 06 '11 at 18:47
  • 1
    @jmein: Argue if you must, but if every single answer and close-voter missed that part, the question needs clarification. I don't care one way or another, it's in OP's best interest. – Wesley Murch Jul 06 '11 at 18:53
  • 1
    @Wesley Murch I agree his question was badly worded, but he definitely specified that it was part of the problem. I have edited the question to clarify his issue. – Josh Mein Jul 06 '11 at 19:00

3 Answers3

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();
Josh Johnson
  • 10,729
  • 12
  • 60
  • 83
  • And how to make it move? And I don't want to change a title if the person is in the tab. – good_evening Jul 06 '11 at 18:27
  • and how I determine if person is in chat tab or not? – good_evening Jul 06 '11 at 18:32
  • The details would be pretty implementation specific, but something along those lines should work in general. – Josh Johnson Jul 06 '11 at 18:39
  • 3
    When you say tab, are you talking about a div tab, or browser tab. If it is browser tab you are talking about, you will have to devise some interesting ways of detecting mouse movement or other inputs or focuses. That should almost be a completely seperate question if this is the case. – Caimen Jul 06 '11 at 19:04
  • @Caimen: you are genius, you gave me a great idea, thanks. :) – good_evening Jul 06 '11 at 19:21
2

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

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
0

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

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188