Created
May 27, 2012 15:33
-
-
Save stryju/2814801 to your computer and use it in GitHub Desktop.
add favicon to anchor tags via css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1st of all - enhance $.unique, so it works for normal arrays | |
// via http://paulirish.com/2010/duck-punching-with-jquery/ | |
(function($){ | |
var _old = $.unique; | |
$.unique = function(arr){ | |
// do the default behavior only if we got an array of elements | |
if (!!arr[0].nodeType){ | |
return _old.apply(this,arguments); | |
} else { | |
// reduce the array to contain no dupes via grep/inArray | |
return $.grep(arr,function(v,k){ | |
return $.inArray(v,arr) === k; | |
}); | |
} | |
}; | |
})(jQuery); | |
// now the method | |
// keep in mind, that it will not add the padding on the left side | |
function addFaviconsToLinks( $parent ){ | |
var $links = $( "a[href^=\"http://\"]", $parent||"body" ), | |
domains = $.unique( $links.map( function( i, e ){ | |
return $( e ).data( "domain", e.href.match( /http\:\/\/([^\/]+)\//i )[ 1 ] ).data( "domain" ); | |
})), | |
bg = "http://favicon.yandex.net/favicon/" + domains.join( "/" ); | |
$links.each( function( key ){ | |
var $this = $( this ); | |
$this.css( "background", [ "url(", bg, ") no-repeat 0 ", -16 * $.inArray( $this.data( "domain" ), domains ), "px" ].join("")); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment