load scripts asynchronously

前端 未结 19 1022
粉色の甜心
粉色の甜心 2020-11-22 12:01

I am using several plugins, custom widgets and some other libraries from JQuery. as a result I have several .js and .css files. I need to create a loader for my site because

相关标签:
19条回答
  • 2020-11-22 12:45

    I wrote a little post to help out with this, you can read more here https://timber.io/snippets/asynchronously-load-a-script-in-the-browser-with-javascript/, but I've attached the helper class below. It will automatically wait for a script to load and return a specified window attribute once it does.

    export default class ScriptLoader {
      constructor (options) {
        const { src, global, protocol = document.location.protocol } = options
        this.src = src
        this.global = global
        this.protocol = protocol
        this.isLoaded = false
      }
    
      loadScript () {
        return new Promise((resolve, reject) => {
          // Create script element and set attributes
          const script = document.createElement('script')
          script.type = 'text/javascript'
          script.async = true
          script.src = `${this.protocol}//${this.src}`
    
          // Append the script to the DOM
          const el = document.getElementsByTagName('script')[0]
          el.parentNode.insertBefore(script, el)
    
          // Resolve the promise once the script is loaded
          script.addEventListener('load', () => {
            this.isLoaded = true
            resolve(script)
          })
    
          // Catch any errors while loading the script
          script.addEventListener('error', () => {
            reject(new Error(`${this.src} failed to load.`))
          })
        })
      }
    
      load () {
        return new Promise(async (resolve, reject) => {
          if (!this.isLoaded) {
            try {
              await this.loadScript()
              resolve(window[this.global])
            } catch (e) {
              reject(e)
            }
          } else {
            resolve(window[this.global])
          }
        })
      }
    }
    

    Usage is like this:

    const loader = new Loader({
        src: 'cdn.segment.com/analytics.js',
        global: 'Segment',
    })
    
    // scriptToLoad will now be a reference to `window.Segment`
    const scriptToLoad = await loader.load()
    
    0 讨论(0)
提交回复
热议问题