utils.js 1.47 KB
Newer Older
JingChao's avatar
JingChao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/**
 * 一些帮助函数
 * @author momoko
 */

/**
 * 取URL上的参数
 * @param  {String} param 参数名
 * @return {String}
 */
export function getUrlParam (param) {
  const result = window.location.href.match(new RegExp('(\\?|&)' + param + '(\\[\\])?=([^&#]*)'))
  return result ? result[3] : undefined
}

/**
 * 动态插入 script to html
 * @param url
 * @param callback
 */
export function createScript (url, callback) {
  const oScript = document.createElement('script')
  oScript.type = 'text/javascript'
  oScript.async = true
  oScript.src = url

  /**
   * IE6/7/8                -- onreadystatechange
   * IE9/10                 -- onreadystatechange, onload
   * Firefox/Chrome/Opera   -- onload
   */

  const isIE = !-[1,] // eslint-disable-line
  if (isIE) {
    // 判断IE8及以下浏览器
    oScript.onreadystatechange = function () {
      if (this.readyState === 'loaded' || this.readyState === 'complete') {
        callback && callback()
      }
    }
  } else {
    // IE9及以上浏览器,Firefox,Chrome,Opera
    oScript.onload = function () {
      callback && callback()
    }
  }

  document.body.appendChild(oScript)
}

/**
 * 判断平台
 * @return {String} 平台
 */
export function detectOS () {
  const ua = navigator.userAgent.toLowerCase()

  if (/MicroMessenger/i.test(ua)) {
    return 'weixin'
  } else if (/iPhone|iPad|iPod|iOS/i.test(ua)) {
    return 'ios'
  } else if (/Android/i.test(ua)) {
    return 'android'
  } else {
    return 'other'
  }
}