children117cl
2021-06-25 5041a24a198686f8c93e0adf497f0f638b6c49c4
提交 | 用户 | age
4dde14 1 let callbacks = []
L 2
3 function loadedTinymce() {
4   // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2144
5   // check is successfully downloaded script
6   return window.tinymce
7 }
8
9 const dynamicLoadScript = (src, callback) => {
10   const existingScript = document.getElementById(src)
11   const cb = callback || function() {}
12
13   if (!existingScript) {
14     const script = document.createElement('script')
15     script.src = src // src url for the third-party library being loaded.
16     script.id = src
17     document.body.appendChild(script)
18     callbacks.push(cb)
19     const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd
20     onEnd(script)
21   }
22
23   if (existingScript && cb) {
24     if (loadedTinymce()) {
25       cb(null, existingScript)
26     } else {
27       callbacks.push(cb)
28     }
29   }
30
31   function stdOnEnd(script) {
32     script.onload = function() {
33       // this.onload = null here is necessary
34       // because even IE9 works not like others
35       this.onerror = this.onload = null
36       for (const cb of callbacks) {
37         cb(null, script)
38       }
39       callbacks = null
40     }
41     script.onerror = function() {
42       this.onerror = this.onload = null
43       cb(new Error('Failed to load ' + src), script)
44     }
45   }
46
47   function ieOnEnd(script) {
48     script.onreadystatechange = function() {
49       if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
50       this.onreadystatechange = null
51       for (const cb of callbacks) {
52         cb(null, script) // there is no way to catch loading errors in IE8
53       }
54       callbacks = null
55     }
56   }
57 }
58
59 export default dynamicLoadScript