Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
window.jsonpCbs = {};
var jsonp = function(uri, cb) {
var key = 'cb' + Math.floor( Math.random() * 100000 );
var cb2 = function() {
delete window.jsonpCbs[key];
cb.apply(null, arguments);
};
window.jsonpCbs[key] = cb2;
var scriptEl = document.createElement('script');
scriptEl.setAttribute('type', 'text/javascript');
var sep = (uri.indexOf('?') === -1) ? '?' : '&';
scriptEl.setAttribute('src', [uri, sep, 'jsonp=jsonpCbs.', key].join(''));
document.head.appendChild(scriptEl);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment