Skip to content

Instantly share code, notes, and snippets.

@Dafrok
Last active August 28, 2017 06:08
Show Gist options
  • Select an option

  • Save Dafrok/443187420034d4564fcb41ece43450e1 to your computer and use it in GitHub Desktop.

Select an option

Save Dafrok/443187420034d4564fcb41ece43450e1 to your computer and use it in GitHub Desktop.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.getJSONP = factory());
}(this, function () { 'use strict';
var global = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this;
return function getJSONP (url, callback) {
var dt = new Date();
var callbackName = 'JSONP_' + Date.parse(dt) + dt.getMilliseconds();
global[callbackName] = callback;
var ref = global.document.getElementsByTagName('script')[0];
var script = global.document.createElement('script');
script.src = url + (url.indexOf('?') + 1 ? '&' : '?') + 'callback=' + callbackName;
ref.parentNode.insertBefore(script, ref);
script.onload = function () {
delete(global[callbackName]);
this.remove();
};
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment