Skip to content

Instantly share code, notes, and snippets.

@yahelc
Created December 1, 2011 04:09
Show Gist options
  • Save yahelc/1413508 to your computer and use it in GitHub Desktop.
Save yahelc/1413508 to your computer and use it in GitHub Desktop.
SharedCount JSONP/CORS jQuery plugin - Cache friendly
jQuery.sharedCount = function(url, fn) {
url = encodeURIComponent(url || location.href);
var arg = {
url: "//" + (location.protocol == "https:" ? "sharedcount.appspot" : "api.sharedcount") + ".com/?url=" + url,
cache: true,
dataType: "json"
};
if ('withCredentials' in new XMLHttpRequest) {
arg.success = fn;
}
else {
var cb = "sc_" + url.replace(/\W/g, '');
window[cb] = fn;
arg.jsonpCallback = cb;
arg.dataType += "p";
}
return jQuery.ajax(arg);
};
jQuery.sharedCount=function(b,c){var b=encodeURIComponent(b||location.href),a={url:"//"+("https:"==location.protocol?"sharedcount.appspot":"api.sharedcount")+".com/?url="+b,cache:!0,dataType:"json"};if("withCredentials"in new XMLHttpRequest)a.success=c;else{var d="sc_"+b.replace(/\W/g,"");window[d]=c;a.jsonpCallback=d;a.dataType+="p"}return jQuery.ajax(a)};
jQuery(document).ready(function($){
$.sharedCount(location.href, function(data){
$("#tweets").text(data.Twitter);
$("#likes").text(data.Facebook.like_count);
$("#plusones").text(data.GooglePlusOne);
$("#sharedcount").fadeIn();
});
});
@nicinabox
Copy link

Shouldn't line 11 include a slash in the regex? \W?

var cb = "sc_" + url.replace(/\W/g, '');

@yahelc
Copy link
Author

yahelc commented Apr 20, 2012

@nicinabox Yes, thanks! For some reason it got removed in the last edit. I've reverted it.

@engagedcount
Copy link

Hi,
SharedCount is no longer accepting new signups. We're providing an API compatible alternative at https://engagedcount.com/

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