Last active
February 10, 2021 07:06
-
-
Save w3collective/7d272c87836dff3d5b6f43b34212e431 to your computer and use it in GitHub Desktop.
Change a website favicon dynamically using 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
const [month, day] = new Date().toLocaleDateString("en-US").split("/"); | |
let favicon; | |
switch (month + day) { | |
case "214": | |
favicon = "๐"; | |
break; | |
case "1031": | |
favicon = "๐"; | |
break; | |
case "1225": | |
favicon = "๐ ๐ผ"; | |
break; | |
default: | |
favicon = "๐"; | |
} | |
const dynamicFavicon = (favicon) => { | |
const link = document.createElement("link"); | |
link.rel = "shortcut icon"; | |
link.type = "image/svg+xml"; | |
link.href = "data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>" + | |
favicon + | |
"</text></svg>"; | |
document.head.appendChild(link); | |
}; | |
dynamicFavicon(favicon); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source => https://w3collective.com/dynamic-favicon/