Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created August 20, 2008 14:04
Show Gist options
  • Save dieseltravis/6367 to your computer and use it in GitHub Desktop.
Save dieseltravis/6367 to your computer and use it in GitHub Desktop.
/**
* Favicons 1.0.th
*
* Prepends or appends a favicon image to all external links
*
* Usage: jQuery.favicons();
*
* @class favicons
* @param {Object} conf, custom config-object
*
* Copyright (c) 2008 Andreas Lagerkvist (andreaslagerkvist.com)
* Released under a GNU General Public License v3 (http://creativecommons.org/licenses/by/3.0/)
*
* Travis's Changes:
* - Updated filter for external links using http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links
* - Customized config
* - Extended for https support
* - Configurable ALT text
*/
jQuery.favicons = function(conf) {
var config = {
insert: 'append',
defaultIco: '/images/external.png',
alternateText: 'external site icon'
};
config = jQuery.extend(config, conf);
jQuery('a[href]').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).each(function() {
var $link = jQuery(this);
var faviconUrl = $link.attr('href').replace(/^(https?:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';
var $faviconImg = jQuery('<img src="' + config.defaultIco +'" alt="' + config.alternateText +'" />')[config.insert +'To']($link);
var extImg = new Image();
extImg.src = faviconUrl;
if(extImg.complete) {
$faviconImg.attr('src', faviconUrl);
}
else {
extImg.onload = function() {
$faviconImg.attr('src', faviconUrl);
};
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment