【问题标题】:How to dynamically change tab title when it is not selected?未选中时如何动态更改选项卡标题?
【发布时间】:2015-09-01 22:40:18
【问题描述】:

我想在浏览器(chrome)中运行的 javascript 游戏的窗口选项卡中显示动态信息(分数):我的目标是在不同的选项卡中运行多个游戏实例,并行运行,并且能够在选项卡标题中查看当前分数。我试过了:

document.title = 分数

...但它仅在选定的选项卡中有效,另一个在选定之前不会刷新(尽管游戏在后台运行良好)。

==> 有没有办法强制更新选项卡标题...即使没有选择?

【问题讨论】:

    标签: javascript dom dynamic title


    【解决方案1】:

    我在http://stackoverflow.com 上发现了几个相同的问题,但它们对我不起作用。

    您可以在这里找到您的解决方案:http://www.raymondcamden.com/2010/10/19/Using-JavaScript-to-update-the-browser-window-title-when-the-user-is-away

    所以,基本上这种代码可以工作:

    var focused = true;
    var baseTitle = "";
    var chatsMissed = 0;
    
    //I'm the fake function that represents some process. We randomly determine if a new chat happened
    function fakeStuff() {
    if(Math.random() > 0.5) {
    if(!focused) {
    chatsMissed++;
    window.document.title = baseTitle + " ("+chatsMissed+")";
    }
    }
    }
    
    $(document).ready(function() {
    
    //store the base title
    baseTitle = window.document.title;
    
    //When the window is focused...
    $(window).focus(function() {
    focused = true;
    //  window.document.title = baseTitle;
    //chrome bug: http://heyman.info/2010/oct/7/google-chrome-bug-when-setting-document-title/
    setTimeout(function() {
    document.title = baseTitle;
    }, 100);
    
    chatsMissed = 0;
    });
    
    //When the window is blurred...
    $(window).blur(function() {
    focused = false;
    });
    
    //setup a process
    window.setInterval('fakeStuff()',2000);
    })
    

    不幸的是 JSfiddle 不支持标题更改。但我测试过,它确实有效。

    【讨论】:

    • 经过一番麻烦,终于成功了,感谢您的帮助。简短的回答是添加: function timer() { window.document.title = score; } window.setInterval(timer,2000);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多