Skip to content

Instantly share code, notes, and snippets.

@neocotic
Last active January 19, 2021 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neocotic/1437732 to your computer and use it in GitHub Desktop.
Save neocotic/1437732 to your computer and use it in GitHub Desktop.
Simple JSONP support
// 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));
@neocotic
Copy link
Author

This has been improved upon and is now a full project; jsonp.js.

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