简体   繁体   中英

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. Changing favicons in some browsers can be tricky. See stackoverflow.com/questions/260857/changing-website-favicon-dynamically/260876#260876 for more info.

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:

How to tell if browser/tab is active

Start with this next line in your <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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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