Posted By

vagrantradio on 02/19/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fenta23


Add favicon to external links with jQuery


 / Published in: jQuery
 

  1. $('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="favicon" />')['prependTo'](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  

You need to login to post a comment.