Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active August 29, 2015 13:56
Show Gist options
  • Save JosePedroDias/8928193 to your computer and use it in GitHub Desktop.
Save JosePedroDias/8928193 to your computer and use it in GitHub Desktop.
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