Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Created August 5, 2013 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rich-Harris/6155282 to your computer and use it in GitHub Desktop.
Save Rich-Harris/6155282 to your computer and use it in GitHub Desktop.
jsonp helper
(function ( global ) {
'use strict';
var jsonp = function ( url, callback ) {
var script, callbackName = 'jsonp_callback_' + Math.floor( Math.random() * 1000000 );
window[ callbackName ] = function ( data ) {
callback( data );
delete window[ callbackName ];
};
script = document.createElement( 'script' );
script.src = url + callbackName;
document.body.appendChild( script );
};
// export as CommonJS module...
if ( typeof module !== 'undefined' && module.exports ) {
module.exports = jsonp;
}
// ... or as AMD module...
else if ( typeof define !== 'undefined' && define.amd ) {
define( function () { return jsonp; });
}
// ... or as browser global
else {
global.jsonp = jsonp;
}
}( this ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment