/**
* 获取浏览器信息
* @returns {Object} {name: String, version: String}
*/
export function getBrowserInfo() {
const browserInfo = {}
const str = navigator.userAgent.toLowerCase()
const isIE = str.includes('compatible') && str.includes('msie') // 判断是否IE<11浏览器
const isIE11 = str.includes('trident') && str.includes('rv:11.0')
const isEdge = str && str.includes('edg/')
if (isIE) {
const reg = new RegExp('msie (\\d+\\.\\d+);')
reg.test(str)
const ieVersion = parseInt(RegExp['$1'])
browserInfo.name = 'ie'
browserInfo.version = ieVersion > 7 ? `ie${ieVersion}` : '<ie7'
} else if (isIE11) {
browserInfo.name = 'ie'
browserInfo.version = 'ie11'
} else if (isEdge) {
const reg = /(edg).*?([\d.]+)/
const info = str.match(reg)
browserInfo.name = info[1]
browserInfo.version = info[2]
} else {
const reg = /(edg|chrome|opera|firefox|version).*?([\d.]+)/
const info = str.match(reg)
browserInfo.name = info[1].replace(/version/, "'safari")// name---类型
browserInfo.version = info[2]// version---版本
}
return browserInfo
}
标签:const,String,JS,str,浏览器,版本信息
From: https://www.cnblogs.com/frank-link/p/17163316.html