Skip to content

Instantly share code, notes, and snippets.

@radusuciu
Created August 8, 2012 16:55
Show Gist options
  • Save radusuciu/3296637 to your computer and use it in GitHub Desktop.
Save radusuciu/3296637 to your computer and use it in GitHub Desktop.
(function (jsonp, d) {
console.log('json-p request');
var request = 0,
callbacks = {};
// takes callback object of the form:
// {
// name : 'customCallback',
// fun : myCallbackFunction
// }
// this allows for callbacks with names other than the generic 'callback' or 'jsonp'
jsonp.get = function(url, data, callback) {
var head = Alt.getTag('head')[0],
script = d.createElement('script');
data[callback.name] = 'Alt.jsonp.callbacks.request_' + request;
callbacks['request_' + request] = function(data) {
head.removeChild(script);
delete callbacks['request_' + request];
callback.fun(data);
};
request++;
if (!Alt.util.isEmpty(data)) url += Alt.util.encodeParams(data);
script.type = 'text/javascript';
script.async = true;
script.src = url;
head.appendChild(script);
};
jsonp.callbacks = callbacks;
}(Alt.jsonp = Alt.jsonp || {}, document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment