Home >Web Front-end >JS Tutorial >How Can I Dynamically Change a Website's Favicon Using JavaScript?
In the realm of web development, customizing the user experience often involves aligning the website's appearance with the logged-in user's preferences. One aspect that may need customization is the website's favicon. This tiny but impactful icon represents the brand in browser tabs and bookmarks.
The task of dynamically changing the favicon based on the private label arises as a challenge. You may have envisioned a solution that involves storing a set of icons and referencing the appropriate one during HTML page generation.
Thankfully, there's a straightforward JavaScript solution to this problem:
var link = document.querySelector("link[rel~='icon']"); if (!link) { link = document.createElement('link'); link.rel = 'icon'; document.head.appendChild(link); } link.href = 'https://stackoverflow.com/favicon.ico';
In this code:
By dynamically updating the href attribute, you can switch the favicon displayed in the browser tab or bookmark. This effectively customizes the user experience by aligning the website's branding with the active user's preferences.
The above is the detailed content of How Can I Dynamically Change a Website's Favicon Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!