判断一个脚本已经加载完毕的方法

export const appendScriptReady = (url, onReadyCallback) => {
  const targetScript = document.createElement('script')
  targetScript.type = 'text/javascript'
  if (targetScript.readyState) { // IE 6,7,8
    targetScript.onreadystatechange = () => {
      if (targetScript.readyState === 'loaded' ||
        targetScript.readyState === 'complete') {
        targetScript.onreadystatechange = null
        onReadyCallback()
      }
    }
  } else { // Not IE
    targetScript.onload = () => {
      onReadyCallback()
    }
  }

  targetScript.src = url
  document.head.appendChild(targetScript)
}

 

posted @ 2018-08-08 15:28  Amy_Li  阅读(555)  评论(0编辑  收藏  举报