Add favicon to external links with jQuery


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.