Skip to content

Instantly share code, notes, and snippets.

@SamirTalwar
Created October 21, 2016 11:43
Show Gist options
  • Select an option

  • Save SamirTalwar/44ad42f030bf98cdee8be6c5c91c39aa to your computer and use it in GitHub Desktop.

Select an option

Save SamirTalwar/44ad42f030bf98cdee8be6c5c91c39aa to your computer and use it in GitHub Desktop.
A quickly-implemented, barely-tested JSONP client implementation, without jQuery.
function getJSONP(address, callback) {
const random = Math.floor(Math.random() * 65536)
const callbackName = 'getJSONP_callback_' + random
window[callbackName] = function() {
document.body.removeChild(script)
delete window[callbackName]
callback.apply(null, arguments)
}
const script = document.createElement('script')
script.src = address.replace('${callback}', callbackName)
document.body.appendChild(script)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment