Skip to content

Instantly share code, notes, and snippets.

@mataspetrikas
Created November 25, 2010 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mataspetrikas/715823 to your computer and use it in GitHub Desktop.
Save mataspetrikas/715823 to your computer and use it in GitHub Desktop.
cacheable JSONP
(function($){
// simple js string hashing
var simpleHash = function(s, tableSize) {
var i, hash = 0;
for (i = 0, l = s.length; i < l; i += 1) {
hash += (s[i].charCodeAt() * (i+1));
}
return Math.abs(hash) % tableSize;
};
$.getJSONP = function(url, callback) {
var callbackName = 'jsonp' + simpleHash(url, Math.pow(10,20));
$.getJSONP.callbacks = $.getJSONP.callbacks || {};
$.getJSONP.callbacks[callbackName] = callback;
$.ajax({url:url, jsonpCallback: '$.getJSONP.callbacks.' + callbackName, dataType: 'jsonp'});
};
})(jQuery);
var url = 'http://api.soundcloud.com/users/matas/tracks.json?consumer_key=mdVdGxuDlepZrVwRYbo4Q';
$.getJSONP(url, function(response){
console.log('the JSONP response:', response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment