/**
*@Date:2021/01/08
*@Description:获取判断浏览器版本信息
*@return {object} or @return {number}
*/
function browserVersion () {
var userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 // 判断是否IE<11浏览器
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1
var isEdge = userAgent.indexOf('Edge') > -1 && !isIE // Edge浏览器
var isFirefox = userAgent.indexOf('Firefox') > -1 // Firefox浏览器
var isOpera = userAgent.indexOf('Opera') > -1 || userAgent.indexOf('OPR') > -1 // Opera浏览器
var isChrome = userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Edge') === -1 && userAgent.indexOf('OPR') === -1 // Chrome浏览器
var isSafari = userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Chrome') === -1 && userAgent.indexOf('Edge') === -1 && userAgent.indexOf('OPR') === -1 // Safari浏览器
if (isIE) {
var reIE = new RegExp('MSIE (\\d+\\.\\d+);')
reIE.test(userAgent)
var fIEVersion = parseFloat(RegExp['$1'])
if (fIEVersion === 7) {
return { type: 'IE', version: 7 }
} else if (fIEVersion === 8) {
return { type: 'IE', version: 8 }
} else if (fIEVersion === 9) {
return { type: 'IE', version: 9 }
} else if (fIEVersion === 10) {
return { type: 'IE', version: 10 }
} else {
return { type: 'IE', version: 6 } // IE版本<7时
}
} else if (isIE11) {
return { type: 'IE', version: 11 }
} else if (isEdge) {
var edgeVersion = userAgent.split('Edge/')[1].split('.')[0]
return { type: 'Edge', version: edgeVersion }
} else if (isFirefox) {
var firefoxVersion = userAgent.split('Firefox/')[1].split('.')[0]
return { type: 'Firefox', version: firefoxVersion }
} else if (isOpera) {
var operaVersion = userAgent.split('OPR/')[1].split('.')[0]
return { type: 'Opera', version: operaVersion }
} else if (isChrome) {
var chromeVersion = userAgent.split('Chrome/')[1].split('.')[0]
return { type: 'Chrome', version: chromeVersion }
} else if (isSafari) {
var safariVersion = userAgent.split('Safari/')[1].split('.')[0]
return { type: 'Safari', version: safariVersion }
} else {
return -1 // 未判断出
}
}
var browserVersionInfo = browserVersion()
// console.log(browserVersionInfo, '浏览器版本判断结果')
// 判断浏览器版本做出相应处理
if (browserVersionInfo !== -1) {
if (browserVersionInfo.type === 'IE' && browserVersionInfo.version < 11) {
window.location = window.location.protocol + '//' + window.location.host + '/noSupport.html'
}
}
js判断浏览器版本及种类
最新推荐文章于 2024-07-26 10:14:05 发布