Posted By

batamire on 08/24/09


Tagged

link jquery external favicon


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

esritter
legacye
vagrantradio
alejandrosuch
Balamir


Add a favicon near external links with jQuery


 / Published in: jQuery
 

URL: http://blog.liviuholhos.com/javascript/add-a-favicon-near-external-links-with-jquery

From Liviu HolhoĊŸ - Blog

http://blog.liviuholhos.com/javascript/add-a-favicon-near-external-links-with-jquery

  1. jQuery('a[href^="http://"]').filter(function(){
  2. return this.hostname && this.hostname !== location.hostname;
  3. }).each(function() {
  4. var link = jQuery(this);
  5. var faviconURL =
  6. link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1')+'/favicon.ico';
  7. var faviconIMG = jQuery('<img src="favicon.png" alt="" />')['appendTo'](link);
  8. var extImg = new Image();
  9. extImg.src = faviconURL;
  10. if (extImg.complete)
  11. faviconIMG.attr('src', faviconURL);
  12. else
  13. extImg.onload = function() { faviconIMG.attr('src', faviconURL); };
  14. });

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: semplice on January 19, 2010

There's a better method here which uses the Google favicon cache, and 4-line script.

You need to login to post a comment.