-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadScript.js
29 lines (26 loc) · 1.01 KB
/
loadScript.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
function addScript(url, callback) {
var script = docElement.createElement("script"),
scriptLoaded = 0;
// IE和opera支持onreadystatechange
// safari、chrome、opera支持onload
script.onload = script.onreadystatechange = function() {
// 避免opera下的多次调用
if (scriptLoaded) {
return;
};
var readyState = script.readyState;
if ('undefined' == typeof readyState || readyState == "loaded" || readyState == "complete") {
scriptLoaded = 1;
try {
callback();
} finally {
script.onload = script.onreadystatechange = null;
script.parentNode.removeChild(script);
}
}
};
script.asyn = 1;
script.src = url;
var lastScript = docElement.getElementsByTagName("script")[0];
lastScript.parentNode.insertBefore(script, lastScript);
}