-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathloadscript.js
More file actions
34 lines (31 loc) · 901 Bytes
/
Copy pathloadscript.js
File metadata and controls
34 lines (31 loc) · 901 Bytes
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
/*
* 动态加载js文件
* @param string path js文件路径
* @param funciong callback 回调方法(返回参数true和false)
*/
function loadScript(path, callback) {
const script = document.createElement('script');
script.type = 'text/javascript';
const head = document.head;
const error = () => {
script.onload = script.onreadystatechange = script.onerror = null;
head.removeChild(script);
callback(false);
}
const success = () => {
script.onload = script.onreadystatechange = script.onerror = null;
head.removeChild(script);
callback(true);
}
script.onreadystatechange = function () {
if (this.readyState == "loaded" || this.readyState == "complete") {
success();
}
};
script.onload = success;
script.onerror = error;
script.src = path;
head.appendChild(script);
}
exports.loadScript = loadScript;
exports.__esModule = true;