The Wayback Machine - https://web.archive.org/web/20250604231442/https://www.geeksforgeeks.org/how-to-change-favicon-dynamically/
Open In App

How to Change Favicon Dynamically?

Last Updated : 26 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

You may probably notice that many social networks and modern web applications have dynamic favicons. Dynamic favicons may be for different purposes, for example, social networks use it to display notifications or messages. Some websites also have the percentage-based favicon that tracks the levels of success, and if you complete a level, the favicon is replaced with a success icon. The favicon is changed to grab people’s attention. You can rotate between different icons for different pages of your web application.

Approach

To change favicons dynamically, we will create two JavaScript functions, to change favicons GeeksforGeeks and Technical Scripter respectively. We will assign a constant variable and get them by the favicon Id with the getElementById() function. After that, we will create 2 functions and assign two buttons for that. By hovering those buttons favicon will change.

Example: To begin with, we will create an HTML page with two buttons on it. These buttons will help to change the favicon using mouse hover.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Dynamic Favicon</title>

    <!-- Default favicon image -->
    <link id="favicon" rel="icon"
          href=
"https://media.geeksforgeeks.org/wp-content/uploads/20240605120007/GFG-logo.jpg"
          type="image/png" sizes="16x16">

    <!--To style the Buttons-->
    <style>
        button {
            background-color: green;
            padding: 15px;
            color: white;
            border-radius: 15px;
        }

        button:hover {
            background-color: rgb(115, 207, 115);
            color: #222;
        }

        .btn1 {
            border: none;

        }

        .btn2 {
            border: none;
        }

        .container {
            text-align: center;
        }

        h1 {
            color: green;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>GeeksforGeeks</h1>
        <h3>Change favicon with JavaScript</h3>

        <!--Creating buttons to change favicons on 
        the hover of the mouse on the button-->
        <button class="btn1" type="button" onmouseover="toRed()">
            GeeksforGeeks
        </button>
        <button class="btn2" type="button" onmouseover="toBlue()">
            Tecnical Scripter
        </button>

        <script>

            // Assign a constant variable and get them by the favicon Id
            const favicon = document.getElementById("favicon");

            // Creating a function for the button GeeksforGeeks
            function toRed() {
                favicon.setAttribute("href", 
"https://media.geeksforgeeks.org/wp-content/uploads/20240524060236/Gfg-final-logo.png");
            }
            // Creating a function for the button Techiniacl Script
            function toBlue() {
                favicon.setAttribute("href", 
"https://media.geeksforgeeks.org/wp-content/uploads/20240510162817/logo.png");
            }
        </script>
    </div>
</body>

</html>


Output:


dynamic_faviIcon
Output

Next Article

Similar Reads