Skip to content

Instantly share code, notes, and snippets.

@EITANINOMIYA
Last active January 24, 2017 10:40
Show Gist options
  • Save EITANINOMIYA/ff19146a1af3509d3edf62abc70a76ee to your computer and use it in GitHub Desktop.
Save EITANINOMIYA/ff19146a1af3509d3edf62abc70a76ee to your computer and use it in GitHub Desktop.
【javascript】facebookのいいね数を取得する
/*#################################################
# httpとhttpsのいいね数を合算するサンプル
#################################################*/
var target = ".js-facebook_count";
var href = location.href.split(":")[1];
/**
* いいね数を取得する
*/
function getShareCount(href) {
return $.ajax({
dataType : "jsonp",
url : "https://graph.facebook.com/",
data : {
id : href
}
})
}
// 合算していいね数を表示するするサンプル
$.when(getShareCount("http:"+href), getShareCount("https:"+href))
.done(function(res1,res2) {
var count = 0;
if(typeof res1[0].share != "undefined") {
count += res1[0].share.share_count;
}
if(typeof res2[0]["share"] != "undefined") {
count += res2[0].share.share_count;
}
$(target).text(count);
console.log(count)
})
.fail(function(error) {
console.log(error.message)
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment