Skip to content

Instantly share code, notes, and snippets.

@dmolsen
Created September 27, 2010 15:47
Show Gist options
  • Save dmolsen/599243 to your computer and use it in GitHub Desktop.
Save dmolsen/599243 to your computer and use it in GitHub Desktop.
Pulls the Facebook fan count and Twitter follower count for the specified accounts.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var f_page = "wvumountaineers"; // the page name for your fan page, e.g. the 'wvumountaineers' part of http://facebook.com/wvumountaineers
var t_page = "westvirginiau"; // the account name for your main twitter account
function add_commas(number) {
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0)) {
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
} else {
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
}
return (output);
} else {
return number;
}
}
// when document is ready load the counts
$(document).ready(function(){
// grab from facebook
$.getJSON('https://graph.facebook.com/'+f_page+'?callback=?', function(data) {
var fb_count = data['likes'].toString();
fb_count = add_commas(fb_count);
$('#fb_count').html(fb_count);
});
// grab from twitter
$.getJSON('http://api.twitter.com/1/users/show.json?screen_name='+t_page+'&callback=?', function(data) {
twit_count = data['followers_count'].toString();
twit_count = add_commas(twit_count);
$('#twitter_count').html(twit_count);
});
});
</script>
</head>
<body>
<noscript>The following counts are dynamically populated by JavaScript. You can also directly visit the sources to find the counts at http://facebook.com/[pagename] and http://twitter.com/[accountname]</noscript>
Facebook fan count: <span id="fb_count"></span><br />
Twitter follower count: <span id="twitter_count"></span><br />
<br />
<em>You could put these spans in nice little badges or something...</em>
</body>
</html>
@shameermohamed
Copy link

the script is not working for me. Showing the count as 0 always.

@v0ff4k
Copy link

v0ff4k commented Jul 15, 2017

both of this social network's API is updated. So this code is outdated !

@georgeka98
Copy link

georgeka98 commented Sep 30, 2020

Not working

Access to fetch at 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=test' from origin 'http://www.lvh.me:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Code down

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment