export default function (url, callback) {

  function onloaded (script, callback) {
    // 绑定加载完的回调函数
    if (script.readyState) {
      // ie
      script.attachEvent('onreadystatechange', function () {
        if (script.readyState === 'loaded' || script.readyState === 'complete') {
          script.className = 'loaded'
          callback && callback.constructor === Function && callback()
        }
      })
    } else {
      script.addEventListener('load', function () {
        script.className = 'loaded'
        callback && callback.constructor === Function && callback()
      }, false)
    }
  }
  // 非阻塞的加载 后面的js会先执行
  function _setCSS (url, callback) {
    var links = document.getElementsByTagName('link')

    for (var i = 0; i < links.length; i++) {
      // 是否已加载
      if (links[i].href.indexOf(url) > -1) {
        return
      }
    }
    let link = document.createElement('link')

    link.type = 'text/css'
    link.rel = 'stylesheet'
    link.href = url
    let head = document.getElementsByTagName('head')[0]

    head.insertBefore(link, head.getElementsByTagName('link')[0] || null)
  }
  function _setJS (url, callback) {
    var scripts = document.getElementsByTagName('script')

    for (let i = 0; i < scripts.length; i++) {
      // 是否已加载
      if (scripts[i].src.indexOf(url) > -1 && callback && callback.constructor === Function) {
        // 已创建script
        if (scripts[i].className === 'loaded') {
          // 已加载
          callback()
        } else {
          // 加载中
          onloaded(scripts[i], callback)
        }
        return
      }
    }
    var script = document.createElement('script')

    script.type = 'text/javascript'
    script.src = url
    document.body.appendChild(script)
    onloaded(script, callback)
  }
  (/\/.+\.js($|\?)/i).test(url) ? _setJS(url, callback) : _setCSS(url, callback)
}
