Skip to content

Instantly share code, notes, and snippets.

@callum
Last active December 29, 2015 02:09
Show Gist options
  • Save callum/7598640 to your computer and use it in GitHub Desktop.
Save callum/7598640 to your computer and use it in GitHub Desktop.
var getData = new JSONPInterface();
function callback(data) {
}
getData("http://example.com/?callback=", callback);
function JSONPInterface() {
var script;
var fn;
return function(url, callback) {
if (script) {
document.body.removeChild(script);
}
fn = "__callback_" + Math.random().toString(36).substring(7);
window[fn] = function() {
callback.apply(this, arguments);
delete window[fn];
};
script = document.createElement("script");
script.src = url + fn;
document.body.appendChild(script);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment