简体   繁体   English

更改选项卡上的更改图标

[英]Change Favicon on Tab Change

I'd like to change my favicon and browser tab text when a user navigates away to another browser tab. 当用户导航到另一个浏览器标签时,我想更改我的图标和浏览器标签文本。 Does anyone know of a simple way to do this? 有人知道这样做的简单方法吗?

Use the window.onblur event to change document.title and the favicon. 使用window.onblur事件更改document.title和图标。 Changing favicons in some browsers can be tricky. 在某些浏览器中更改网站图标可能很棘手。 See stackoverflow.com/questions/260857/changing-website-favicon-dynamically/260876#260876 for more info. 有关更多信息,请参见stackoverflow.com/questions/260857/changing-website-favicon-dynamically/260876#260876

window.onblur=function(){
    //change favicon
    document.title="bye";
}
window.onfocus=function(){
    document.title="hello";
}

The simplest way to achieve this would be to use javascript to detect if the tab is active: 实现此目的的最简单方法是使用javascript检测选项卡是否处于活动状态:

How to tell if browser/tab is active 如何判断浏览器/标签页是否处于活动状态

Start with this next line in your <head></head> <head></head>下一行开始

<link rel="shortcut icon" id="changingFavicon" type="image/png" href="/favicon.png"/>

Using the javascript example linked above to check if the tab is no longer active change the href to the new directory location like so using jquery. 使用上面链接的javascript示例来检查选项卡是否不再处于活动状态,就像使用jquery一样,将href更改为新目录位置。

$("#changingFavicon").attr("href", "http://newlocation.com/favicon.png");

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

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