Created
August 5, 2013 11:38
-
-
Save Rich-Harris/6155282 to your computer and use it in GitHub Desktop.
jsonp helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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