Quick And Dirty JSONP

I needed JSONP for WordyClouds, and since I am trying not to have to use jQuery, I just rolled my own. JSONP is a simple hack, so this is but a short little snippet:

var JSONP = {}
JSONP.get = function(url, callback){
    var scriptTag = document.createElement('script')
    var callbackName = '_' + new Date().getTime()
    JSONP[callbackName] = function(){
        callback.apply(null, arguments)
        delete JSONP[callbackName]
    if (url.indexOf('?') != -1)
        url += '&callback=JSONP.' + callbackName
        url += '?callback=JSONP.' + callbackName
    scriptTag.src = url

Example usage:

JSONP.get('http://feeds.delicious.com/feeds/json/tags/your_username', function(data){
    // Woohoo! You got data!
blog comments powered by Disqus