Skip to content

Instantly share code, notes, and snippets.

@ninja33
Created January 18, 2018 08:45
Show Gist options
  • Save ninja33/83d351965cec83ca26ebc6526383b636 to your computer and use it in GitHub Desktop.
Save ninja33/83d351965cec83ca26ebc6526383b636 to your computer and use it in GitHub Desktop.
a simple and pure javascript jsonp function
function loadJsonp(url, callback) {
var callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random());
window[callbackName] = function (data) {
delete window[callbackName];
document.body.removeChild(script);
callback(data);
};
var script = document.createElement('script');
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName;
document.body.appendChild(script);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment