-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcreateScript.js
More file actions
35 lines (32 loc) · 819 Bytes
/
createScript.js
File metadata and controls
35 lines (32 loc) · 819 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
33
34
35
/**
* dynamic add script to html
*
* @param url
* @param callback
*/
export default function createScript(url, callback) {
const oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.async = true;
oScript.src = url;
/**
* IE6/7/8 -- onreadystatechange
* IE9/10 -- onreadystatechange, onload
* Firefox/Chrome/Opera -- onload
*/
const isIE = !-[1,];
if (isIE) {
// 判断IE8及以下浏览器
oScript.onreadystatechange = function () {
if (this.readyState == 'loaded' || this.readyState == 'complete') {
callback();
}
}
} else {
// IE9及以上浏览器,Firefox,Chrome,Opera
oScript.onload = function () {
callback();
}
}
document.body.appendChild(oScript);
}