Skip to content

Instantly share code, notes, and snippets.

@jeandcr
Forked from mathiasbynens/change-favicon.js
Created March 15, 2019 13:59
Show Gist options
  • Select an option

  • Save jeandcr/484cfc0f6c135edddd8b681c63a5bec3 to your computer and use it in GitHub Desktop.

Select an option

Save jeandcr/484cfc0f6c135edddd8b681c63a5bec3 to your computer and use it in GitHub Desktop.
Dynamically changing favicons with JavaScript
/*!
* Dynamically changing favicons with JavaScript
* Works in all A-grade browsers except Safari and Internet Explorer
* Demo: http://mathiasbynens.be/demo/dynamic-favicons
*/
// HTML5™, baby! http://mathiasbynens.be/notes/document-head
document.head = document.head || document.getElementsByTagName('head')[0];
// Browser sniffing :`(
// Need to match Chromium and Chrome
if (/Chrom/.test(navigator.userAgent)) {
var isChrome = 1,
iframe = document.createElement('iframe');
iframe.src = 'about:blank';
iframe.style.display = 'none';
document.body.appendChild(iframe);
};
function changeFavicon(src) {
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = src;
if (oldLink) {
document.head.removeChild(oldLink);
};
document.head.appendChild(link);
if (isChrome) {
iframe.src += '';
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment