Add a favicon near external links with jQuery


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

From Liviu HolhoÅŸ - Blog

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


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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.