Last active
January 19, 2021 20:19
-
-
Save neocotic/1437732 to your computer and use it in GitHub Desktop.
Simple JSONP support
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
// Simple JSONP support | |
// (c) 2011 [neocotic](http://github.com/neocotic) | |
// Released under the MIT License | |
// http://en.wikipedia.org/wiki/MIT_License | |
(function (root) { | |
var previousJSONP = root.JSONP; | |
root.JSONP = { | |
__callbacks : {}, | |
get : function (url, callback, context) { | |
var | |
id = +new Date(), | |
script = document.createElement('script'); | |
while (JSONP.__callbacks[id] !== undefined) id += Math.random(); | |
JSONP.__callbacks[id] = function () { | |
delete JSONP.__callbacks[id]; | |
callback.apply(context, arguments); | |
}; | |
url += (url.indexOf('?') === -1) ? '?' : '&'; | |
url += 'callback=' + encodeURIComponent('JSONP.__callbacks[' + id + ']'); | |
script.setAttribute('src', url); | |
document.getElementsByTagName('head')[0].appendChild(script); | |
}, | |
noConflict : function () { | |
root.JSONP = previousJSONP; | |
return this; | |
} | |
}; | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has been improved upon and is now a full project; jsonp.js.