-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIAJSONP.js
38 lines (29 loc) · 1.13 KB
/
IAJSONP.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var IAJSONP = (function () {
var module = {};
module.addJavascriptFile = function (url, context) {
context = context || document;
var head = context.getElementsByTagName('head')[0];
var js = context.createElement('script');
js.src = url;
js.type = "text/javascript";
head.appendChild(js);
return js;
}
module.get = function (url, handle) {
//creating random name of function so not conflict with others
var func = 'IAJSONP' + Math.floor((Math.random() * 1000000000000000000) + 1);
//adding rndomly created function to globle window object
window[func] = function (data) {
//calling handle
handle(data);
//removing random named declared function
window[func] = function () {};
//removing added js
document.head.removeChild(js);
}
//manipulating and adding js file to head
var endurl = url.indexOf('?') != -1 ? url + '&callback=' + func : url + '?callback=' + func;
var js = module.addJavascriptFile(endurl);
}
return module;
})();