Search code examples
javascriptjqueryfavicon

Different Favicon onload?


I'm trying to find away to load a different favicon on page refresh/load. I've had a quick google but noting seems to be showing up in terms of tutorials.


Solution

  • based on the answer from here: Changing website favicon dynamically I created what you want.

    (function() {
        var arrFavicon = ['link_favicon1', 'link_favicon2', 'link_favicon3']; //put your favicon links here
    
        var link = document.createElement('link');
        link.type = 'image/x-icon';
        link.rel = 'shortcut icon';
        link.href = arrFavicon[Math.floor(Math.random()*arrFavicon.length)]; //don't need to change here no matter how many favicons you have
        document.getElementsByTagName('head')[0].appendChild(link);
    }());
    

    Now, all you have to do is put your favicons links inside the array arrFavicon and will automatically generate diferents favicons every time that the page is loaded.