Skip to content

Instantly share code, notes, and snippets.

@chirag64
Created February 19, 2014 16:27
Show Gist options
  • Save chirag64/9095605 to your computer and use it in GitHub Desktop.
Save chirag64/9095605 to your computer and use it in GitHub Desktop.
Use this to get the Facebook & Twitter social share counts of a web page
function twitterCallback(result) {
result.count && console.log('The count is: ', result.count);
}
function getTwitterCount(url) {
var script = document.createElement('script');
script.async = true;
script.src = 'http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=twitterCallback';
document.body.appendChild(script);
}
// Usage: getTwitterCount('http://davidwalsh.name/twitter-facebook-jsonp');
function facebookCallback(result) {
result.shares && console.log('The count is: ', result.shares);
}
function getFacebookCount(url) {
var script = document.createElement('script');
script.async = true;
script.src = 'https://graph.facebook.com/?id=' + url + '&callback=facebookCallback';
document.body.appendChild(script);
}
// Usage: getFacebookCount('http://davidwalsh.name/twitter-facebook-jsonp');
//Credits: David Walsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment