Skip to content

Instantly share code, notes, and snippets.

@lectroidmarc
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lectroidmarc/7f2c42a0f4ef5cde0f58 to your computer and use it in GitHub Desktop.
Save lectroidmarc/7f2c42a0f4ef5cde0f58 to your computer and use it in GitHub Desktop.
/**
* Implement JSONP
* @param {string} url The URL to call.
* @param {function} callback A callback function, called with a single data parameter.
*/
function jsonp(url, callback) {
var callbackName = '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