Created
May 2, 2015 01:52
-
-
Save yuval-a/c13e6af6befa75136122 to your computer and use it in GitHub Desktop.
getJSONP in vanilla JS
This file contains hidden or 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 getJSONP(url, success) { | |
var ud = 'callback_' + + Math.floor(Math.random()*1000000), | |
script = document.createElement('script'), | |
head = document.getElementsByTagName('head')[0] | |
|| document.documentElement; | |
window[ud] = function(data) { | |
head.removeChild(script); | |
window[ud] = null; | |
success && success(data); | |
}; | |
script.src = url.replace('callback=?', 'callback=' + ud); | |
head.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment