-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonp_client.js
43 lines (35 loc) · 1.21 KB
/
jsonp_client.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
39
40
41
42
43
function jsonPClient(urlTarget, success, error) {
successSafe = success || function(){};
errorSafe = error || function(){};
var hash = Math.random().toString()
.replace('0.', '');
var callbackName = 'jsonp_client_' + hash;
window[callbackName] = function(data){
cleanUp();
successSafe(data);
}
var scriptTarget = document.createElement('script');
scriptTarget.type = 'text/javascript';
scriptTarget.async = true;
//scriptTarget.charset = ...
var stamp = new Date().valueOf();
var src = urlTarget + '?callback=' + callbackName + '&stamp=' + stamp;
scriptTarget.src = src;
scriptTarget.onerror = function(a, b, c){
cleanUp();
errorSafe(a, b, c);
};
var scriptHost = getScriptHost();
scriptHost.insertBefore(scriptTarget, scriptHost.firstChild );
function cleanUp(){
delete window[callbackName];
scriptHost.removeChild(scriptTarget);
scriptHost = null;
scriptTarget.onerror = null;
scriptTarget = null;
}
function getScriptHost(){
var scriptHost = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
return scriptHost;
};
};