Get url favicon with Jquery
Question
I found a script which allows to display favicons based on an url over here: andreaslagerkvist
Its a pretty simple script, there is a good example but when I copy/paste the example it doesn't seem to work..Please take a look at this demo: JsFiddle
What am I doing wrong? Am I missing something in the script?
Solution
You never called the plugin, you only defined it.
Plugin Definition
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);
};
}
});
});
};
Plugin Invocation
jQuery('#jquery-favicons-example').favicons({insert: 'insertBefore'});
Here's a working fiddle.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow