Thursday, October 9, 2025
HomeLanguagesJavascriptHow to change favicon dynamically?

How to change favicon dynamically?

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.

To change favicons dynamically, we will create two javascript functions, to change favicons neveropen 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.

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




<!DOCTYPE html>
<html>
  
<head>
    <title>Dynamic Favicon</title>
    
    <!-- Default favicon image -->
    <link id="favicon" rel="icon"
          href="image_name.png"
          type="image/png" 
          sizes="16x16">
    
    <!--To style the Buttons-->
    <style>
        .btn1 {
            background-image: url('img2.png');
            border: none;
              
        }
        .btn2 {
            background-image: url('img.png');
            border: none;
        }
          
        .container {
            text-align: center;
        }
          
        h1 {
            color:green;
        }
    </style>
</head>
  
<body>
    <div class="container">
    <h1>neveropen</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()">
      neveropen
    </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 neveropen
        function toRed() {
            favicon.setAttribute("href", "img2.png"); 
        }
        // Creating a function for the button Techiniacl Script
        function toBlue() {
            favicon.setAttribute("href", "img.png");
        }
    </script>
    </div>
</body>
  
</html>


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS