简体   繁体   English

jQuery 用户在不同选项卡中时更改页面标题

[英]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.当收到新消息并且用户与实时聊天不在同一个选项卡中时,我想更改标题(在 omegle.com 中移动)。 When the user returns to the tab, the title would return to normal.当用户返回选项卡时,标题将恢复正常。

I guess it should be done by jQuery.我想它应该由 jQuery 完成。 Do you know any plugins or how can I do that?你知道任何插件或我该怎么做?

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() ;为了使它成为 flash 你必须用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 .显然,在 IE 中, $('title')[0].innerHTML返回<title>标签的内容,但您不能设置它的值,除非使用document.title I guess this should be an improvement to the jQuery API, since $('title')[0] does return a DOMElement ( nodeType = 1 )...我想这应该是对 jQuery API 的改进,因为$('title')[0]确实返回了一个 DOMElement ( nodeType = 1 )...

$('title').text('your title') suffices. $('title').text('your title')就足够了。

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.要查看您是否走对了路,只需使用 IE 的开发人员工具栏 (F12) 和 go 到控制台并写入$('title') ,您应该会在控制台中看到 [...]。 This means that $('title') is an object and it works up to here.这意味着$('title')是一个 object 并且它可以工作到这里。 Then write typeof $('title').text , and you should see function as the result.然后写typeof $('title').text ,你应该会看到function作为结果。 If these tests are OK, then your IE is broken.如果这些测试没问题,那么你的 IE 就坏了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM