Created
November 25, 2010 19:43
-
-
Save mataspetrikas/715823 to your computer and use it in GitHub Desktop.
cacheable JSONP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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