Skip to content

Instantly share code, notes, and snippets.

@yuval-a
Created May 2, 2015 01:52
Show Gist options
  • Save yuval-a/c13e6af6befa75136122 to your computer and use it in GitHub Desktop.
Save yuval-a/c13e6af6befa75136122 to your computer and use it in GitHub Desktop.
getJSONP in vanilla JS
function getJSONP(url, success) {
var ud = 'callback_' + + Math.floor(Math.random()*1000000),
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
window[ud] = null;
success && success(data);
};
script.src = url.replace('callback=?', 'callback=' + ud);
head.appendChild(script);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment