b02ea27942e7f66f613583f5c23890db34c2e6b3.svn-base 1.6 KB

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