判断浏览器类型,字符串转对象

本文介绍了如何通过JavaScript代码判断用户所使用的浏览器类型,并详细讲解了将字符串转换为JavaScript对象的方法,包括JSON.parse()的使用及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * Created For: 根据访问浏览器 用户代理获取 访问设备、操作系统、浏览器等信息
 * Author: chenLiYan.
 * Date: 2019/5/9 0009 18:13.
 */
function getDeviceInfo () {
  // 第一步判断访问设备是PC还是手机端(ios/Android)
  let userAgent = navigator.userAgent.toLowerCase()
  let browserInfo = getBrowserInfo()
  let result = {
    channel: undefined,
    system: undefined,
    browser: browserInfo.browserType + ' ' + browserInfo.browserVer
  }
  if (/(iphone|ios)/i.test(userAgent)) {
    result.channel = '手机'
    result.system = 'ios'
  } else if (/(ipad)/i.test(userAgent)) {
    result.channel = 'iPad'
    result.system = 'iPad'
  } else if (/(ipod)/i.test(userAgent)) {
    result.channel = 'iPod'
    result.system = 'iPod'
  } else if (/(Android)/i.test(navigator.userAgent)) {
    result.channel = '手机'
    result.system = 'Android'
  } else {
    result.channel = 'PC'
    result.system = detectOS()
  }
  return result
}
// 判断操作系统
function detectOS () {
  let sUserAgent = navigator.userAgent
  let isWin = (navigator.platform === 'Win32') || (navigator.platform === 'Windows')
  let isMac = (navigator.platform === 'Mac68K') || (navigator.platform === 'MacPPC') || (navigator.platform === 'Macintosh') || (navigator.platform === 'MacIntel')
  if (isMac) return 'Mac'
  let isUnix = (navigator.platform === 'X11') && !isWin && !isMac
  if (isUnix) return 'Unix'
  let isLinux = (String(navigator.platform).indexOf('Linux') > -1)
  if (isLinux) return 'Linux'
  if (isWin) {
    let isWin2K = sUserAgent.indexOf('Windows NT 5.0') > -1 || sUserAgent.indexOf('Windows 2000') > -1
    if (isWin2K) return 'Windows 2000'
    let isWinXP = sUserAgent.indexOf('Windows NT 5.1') > -1 || sUserAgent.indexOf('Windows XP') > -1
    if (isWinXP) return 'Windows XP'
    let isWin2003 = sUserAgent.indexOf('Windows NT 5.2') > -1 || sUserAgent.indexOf('Windows 2003') > -1
    if (isWin2003) return 'Windows 2003'
    let isWinVista = sUserAgent.indexOf('Windows NT 6.0') > -1 || sUserAgent.indexOf('Windows Vista') > -1
    if (isWinVista) return 'Windows Vista'
    let isWin7 = sUserAgent.indexOf('Windows NT 6.1') > -1 || sUserAgent.indexOf('Windows 7') > -1
    if (isWin7) return 'Windows 7'
    let isWin10 = sUserAgent.indexOf('Windows NT 10.0') > -1 || sUserAgent.indexOf('Windows 10') > -1
    if (isWin10) return 'Windows 10'
  }
  return 'other'
}
// 获取当前浏览器类型及版本
function getBrowserInfo () {
  let ua = navigator.userAgent.toLocaleLowerCase()
  let browserType = null
  let browserVer = ''
  let re = /(msie|firefox|chrome|opera|netscape|version).*?([\d.]+)/i
  let m = ua.match(re)
  if (m && m[2]) {
    browserVer = m[2]
  }
  if (/micromessenger/i.test(ua)) {
    browserType = '微信浏览器'
    browserVer = ua.match(/micromessenger\/([\d.]+)/i)[1]
  } else if (/dingtalk/i.test(ua)) {
    browserType = '钉钉'
    browserVer = ua.match(/dingtalk\/([\d.]+)/i)[1]
  } else if (ua.match(/firefox/) !== null) {
    browserType = '火狐'
  } else if (ua.match(/ubrowser/) !== null) {
    browserType = 'UC'
    browserVer = ua.match(/ubrowser\/([\d.]+)/i)[1]
  } else if (ua.match(/opera/) !== null) {
    browserType = '欧朋'
  } else if (ua.match(/bidubrowser/) !== null) {
    browserType = '百度'
    browserVer = ua.match(/bidubrowser\/([\d.]+)/i)[1]
  } else if (ua.match(/metasr/) !== null || ua.match(/se 2.x/) !== null) {
    browserType = '搜狗'
    browserVer = ''
  } else if (ua.match(/tencenttraveler/) !== null || ua.match(/qqbrowser/) !== null) {
    browserType = 'QQ'
    browserVer = ua.match(/tencenttraveler\/([\d.]+)/i) !== null ? ua.match(/tencenttraveler\/([\d.]+)/i)[1] : (ua.match(/qqbrowser\/([\d.]+)/i) !== null ? ua.match(/qqbrowser\/([\d.]+)/i)[1] : '')
  } else if (ua.match(/maxthon/) !== null) {
    browserType = '遨游'
    browserVer = ua.match(/maxthon\/([\d.]+)/i)[1]
  } else if (ua.match(/lbbrowser/) !== null) {
    browserType = '猎豹'
    browserVer = ua.match(/lbbrowser\/([\d.]+)/i)[1]
  } else if (ua.match(/oppobrowser/i) !== null) {
    browserType = 'oppo 浏览器'
    browserVer = ua.match(/oppobrowser\/([\d.]+)/i)[1]
  } else if (ua.match(/XiaoMi\/MiuiBrowser/i) !== null) {
    browserType = '小米 浏览器'
    browserVer = ua.match(/MiuiBrowser\/([\d.]+)/i)[1]
  } else if (ua.match(/FxiOS/i) !== null) {
    browserType = '火狐'
    browserVer = ua.match(/FxiOS\/([\d.]+)/i)[1]
  } else if (ua.match(/OPR/i) !== null) {
    browserType = 'Opera'
    browserVer = ua.match(/OPR\/([\d.]+)/i)[1]
  } else if (ua.match(/chrome/) !== null) {
    let is360 = _mime('type', 'application/vnd.chromium.remoting-viewer')
    if (is360) {
      browserType = '360'
      browserVer = ''
    } else {
      browserType = 'chrome'
    }
  } else if (!!window.ActiveXObject || 'ActiveXObject' in window) {
    browserType = 'IE'
    if (!ua.match(/msie/) && ua.match(/trident/) !== null) {
      browserVer = '11+'
    }
  } else if (ua.match(/safari/) !== null) {
    browserType = 'Safari'
    browserVer = ua.match(/safari\/([\d.]+)/i)[1]
  } else {
    browserType = '其他'
    browserVer = ''
  }
  return {
    browserType: browserType,
    browserVer: browserVer
  }
}

// 判断是否是360浏览器
function _mime (option, value) {
  let mimeTypes = navigator.mimeTypes
  for (let mt in mimeTypes) {
    if (mimeTypes[mt][option] === value) {
      return true
    }
  }
  return false
}

export default getDeviceInfo


// 查询字符串转对象
export function param2Obj (url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  )
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值