Favicons 2.0

Created Tuesday 16th of September 2008 by Andreas Lagerkvist
Copyright © 2008 Andreas Lagerkvist (andreaslagerkvist.com)

Please have a look at the "Other Resources" for bug reports or further help on jQuery. Do not post bug reports or feature requests as comments to unrelated articles. If bug reporting on bugtracker.a-framework.org fails, e-mail me.

What it Does

This little plug-in will insert favicons to all external links found on your site. The plug-in scans the URL the link is pointing to for a favicon and, if it can't find one, displays any generic favicon-image you like instead.

How to Use

jQuery(document.body).favicons({insert: 'prependTo'}); would prepend favicons to every external link in the document.

You wanna run the plug-in on a parent-element of the external links. Unless you want certain external links not to have a favicon there's no reason not to run the plug-in on document.body.

Example Code

HTML

<div id="jquery-favicons-example">
<
ul>
    <
li><a href="http://www.codinghorror.com/blog/">Coding Horror</a></li>
    <
li><a href="http://remysharp.com">remy sharp's b:log</a></li>
    <li><a href="http://snook.ca/jonathan/">Snook.ca</a></li>
    <li><a href="http://www.danwebb.net">DanWebb.net</a></li>
    <li><a href="http://www.dustindiaz.com/">DustinDiaz.com</a></li>
</ul>
</div>

JS

jQuery('#jquery-favicons-example').favicons({insert'insertBefore'defaultIcoWEBROOT +'aFramework/Styles/__common/gfx/jquery.favicons.png'});

Source Code

jQuery.fn.favicons = function (conf) {
    var 
config jQuery.extend({
        
insert:        'appendTo'
        
defaultIco'favicon.png'
    
}, conf);

    return 
this.each(function () {
        
jQuery('a[href^="http://"]'this).each(function () {
            var 
link        jQuery(this);
            var 
faviconURL    link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') + '/favicon.ico';
            var 
faviconIMG    jQuery('<img src="' config.defaultIco '" alt="" />')[config.insert](link);
            var 
extImg        = new Image();

            
extImg.src faviconURL;

            if (
extImg.complete) {
                
faviconIMG.attr('src'faviconURL);
            }
            else {
                
extImg.onload = function () {
                    
faviconIMG.attr('src'faviconURL);
                };
            }
        });
    });
};

Download

Plug-in

Requires

Random jQuery Plug-ins

  • Numeric DL

    This tiny plug-in numbers definitions in a definition-list if there are more than one definition for a term. Can be useful for FAQs, actual lists of d...

    Details

  • Super Simple Tabs

    This is an extremely basic tabs-plugin which allows you to create tabbed content from the ever-so-common list of in-page-links. You can set the select...

    Details

  • Ajax Loader

    Use this plug-in when you want to inform your visitors that a certain part of your page is currently loading. The plug-in adds a faded 'loading-div' o...

    Details

More Plug-ins

Recent Comments

  1. Andreas on "SleekPHP User Handling"

    Thank you for showing interest :) Were the docs en...

  2. ciptard on "SleekPHP User Handling"

    Ok, thank you

  3. Andreas on "SleekPHP User Handling"

    I might write a "getting started" article for Slee...

Page cached. Loaded in: 0.0086 second(s).
Last DB change: 2012-02-04 08:04:40
Last file change: 2011-10-03 07:42:35
Cache created: 2012-02-04 10:14:49