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

大兔子大兔子 提交于 2020-01-31 05:47:52

问题


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?


回答1:


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



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




回答3:


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



来源:https://stackoverflow.com/questions/6601163/jquery-change-pages-title-when-user-in-a-different-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!