Created
July 18, 2014 10:25
-
-
Save denysdovhan/6105c57b9d1346e735f6 to your computer and use it in GitHub Desktop.
JSONP fucntion. (Cross-domain request)
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
doJSONP('http://some.url/', function (data) { | |
console.log(data); | |
}); |
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 doJSONP (url, callback) { | |
var request = ('request'+ Math.random()).replace('.',''), | |
head = document.getElementsByTagName('head')[0], | |
script = document.createElement('script'); | |
script.charset = 'UTF-8'; | |
script.src = url + '?callback=' + id; | |
head.appendChild(script); | |
window[id] = function (data) { | |
callback(data); | |
console.log('Callback was called'); | |
delete window[id]; | |
head.removeChild(script); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment