Search code examples
javascriptfavicon

Adding favicons with generic option


I'd like to add favicons to my site using favicon.ico.

however I want to load a generic icon if the site does not provide one.

How can I test for the presense of a favicon and if one is not present manipulate the DOM to point to a generic favicon on the server.


Solution

  • You could use some JavaScript. Adapt to suit...

    var img = document.getElementsByTagName('img')[0],
        favicon = new Image;
    
    favicon.onerror = function() {
        img.src = 'http://some-other-url.com/favicon.ico';
    }
    
    favicon.src = 'http://example.com/favicon.ico';