Easy Automatic Facebook Share Counts (Fully Javascript)

Drop this little gem in your site’s header:

<script>
function numberFormat(x) {
	return x.toString().replace(/B(?=(d{3})+(?!d))/g, ',');
}

jQuery(document).ready(function($){
	$('.trigger_facebook_count').each(function(){
		var url = $(this).attr('data-url');
		var that = $(this);

		$.get('http://graph.facebook.com/' + url, function(data) {
			that.html(numberFormat(data.shares));
		});	
	});
});
</script>

Then, add elements to your page like so:

<div class="trigger_facebook_count" data-url="http://bossip.com"></div>

Remember to change the data-url to whatever URL you are referencing, and viola! You’re done.

Leave a comment

Your email address will not be published. Required fields are marked *