Skip to content

Instantly share code, notes, and snippets.

@denysdovhan
Created July 18, 2014 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denysdovhan/6105c57b9d1346e735f6 to your computer and use it in GitHub Desktop.
Save denysdovhan/6105c57b9d1346e735f6 to your computer and use it in GitHub Desktop.
JSONP fucntion. (Cross-domain request)
doJSONP('http://some.url/', function (data) {
console.log(data);
});
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