-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_service.js
59 lines (53 loc) · 1.68 KB
/
script_service.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const loaded = {}
export function unloadScript (src) {
// TBD
}
export function loadStyle (src) {
return new Promise((resolve, reject) => {
var fileref = document.createElement("link")
fileref.rel = "stylesheet"
fileref.type = 'text/css'
fileref.href = src
if (fileref.readyState) { //IE
fileref.onreadystatechange = () => {
if (fileref.readyState === "loaded" || fileref.readyState === "complete") {
fileref.onreadystatechange = null
loaded[src] = { loaded: true, status: 'Loaded' }
resolve(loaded[src])
}
}
} else { //Others
fileref.onload = () => {
loaded[src] = { loaded: true, status: 'Loaded' }
resolve(loaded[src])
}
}
document.getElementsByTagName("head")[0].appendChild(fileref)
})
}
export function loadScript (src) {
return new Promise((resolve, reject) => {
if (loaded[src]) {
return resolve({ loaded: true, status: 'Already Loaded'} )
}
let script = document.createElement('script')
script.type = 'text/javascript'
script.src = src
if (script.readyState) { //IE
script.onreadystatechange = () => {
if (script.readyState === "loaded" || script.readyState === "complete") {
script.onreadystatechange = null
loaded[src] = { loaded: true, status: 'Loaded' }
resolve(loaded[src])
}
}
} else { //Others
script.onload = () => {
loaded[src] = { loaded: true, status: 'Loaded' }
resolve(loaded[src])
}
}
script.onerror = (error) => resolve({loaded: false, status: error })
document.getElementsByTagName('head')[0].appendChild(script)
})
}