Created
August 1, 2023 15:36
-
-
Save Tynael/7801a6d511518abeb12a861cc2a1ccf1 to your computer and use it in GitHub Desktop.
How to Change Favicon on Tab Switch Using JavaScript
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Website</title> | |
| <link rel="icon" type="image/x-icon" href="favicon.ico" /> | |
| </head> | |
| <body> | |
| <script> | |
| const favicon = document.querySelector('link[rel="icon"]') | |
| document.addEventListener("visibilitychange", () => { | |
| const state = document.hidden ? "-inactive" : ""; | |
| favicon.setAttribute("href", `favicon${state}.ico`) | |
| }) | |
| </script> | |
| </body> | |
| </html> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source code used in https://neutrondev.com/change-favicon-on-tab-switching-javascript/