Waiting for dynamically loaded script

前端 未结 6 1767
忘了有多久
忘了有多久 2020-11-30 08:28

In my page body, I need to insert this code as the result of an AJAX call:

    

Loading jQuery

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 09:00

    const jsScript = document.createElement('script')
    jsScript.src =
      'https://coolJavascript.js'
    
    document.body.appendChild(jsScript)
    
    jsScript.addEventListener('load', () => {
      doSomethingNow()
    })
    

    Will load after the script is dynamically added

提交回复
热议问题