Created
June 7, 2010 12:41
-
Star
(126)
You must be signed in to star a gist -
Fork
(29)
You must be signed in to fork a gist
-
-
Save mathiasbynens/428626 to your computer and use it in GitHub Desktop.
Dynamically changing favicons with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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]); | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a VERY simple way to change the Favicon on your website.
link rel="icon" type="image/x-icon" href="STARTING FAVICON URL GOES HERE" id="icon" / ---- Add < at the start and > at the end for it to work!
<script type="text/javascript"> var url = 'YourURLHere'; function changeIcon() { document.getElementById("icon").href = url; } </script>