Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change a website favicon dynamically using JavaScript
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);
@w3collective

This comment has been minimized.

Copy link
Owner Author

@w3collective w3collective commented Feb 7, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment