识别浏览器方法
Posted zhou195
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了识别浏览器方法相关的知识,希望对你有一定的参考价值。
获取浏览器的类型、版本及操作系统信息
敬上代码,抛砖引玉
var browserInfo = function() { var NT = { ‘4.90‘: ‘Windows ME‘, ‘4.0‘: ‘Windows NT‘, ‘5.0‘: ‘Windows 2000‘, ‘5.01‘: ‘Windows 2000 SP1‘, ‘5.1‘: ‘Windows XP‘, ‘5.2‘: ‘Windows XP 64-bit / Windows Server 2003‘, ‘6.0‘: ‘Windows Vista / Windows Server 2008‘, ‘6.1‘: ‘Windows 7 / Windows Server 2008 R2‘, ‘6.2‘: ‘Windows 8‘, ‘6.3‘: ‘Windows 8.1‘, ‘6.4‘: ‘Windows 10 Technical Preview‘, ‘10.0‘: ‘Windows 10‘ } var userAgent = navigator.userAgent.toLowerCase(); var platform = navigator.platform; var browserName = /(msies|trident.*rv:)([w.]+)/.test(userAgent) ? ‘Internet Explorer‘: (window.opera && window.opera.version ? ‘Opera‘: ((userAgent.indexOf(‘edge‘) > -1) ? ‘Microsoft Edge‘: ((userAgent.indexOf(‘chrome‘) > -1) ? ‘Chrome‘: ((userAgent.indexOf("firefox") > -1) ? ‘Firefox‘: (userAgent.indexOf(‘safari‘) > -1 ? ‘Safari‘: ‘Unknown‘))))); var browserVersion = browserName == ‘Internet Explorer‘ ? (userAgent.indexOf(‘rv:‘) > -1 ? userAgent.match(/rv:[d.]+/gi)[0].replace(‘rv:‘, ‘‘) : userAgent.match(/msies[d.]+/gi)[0].replace(‘msie ‘, ‘‘)) : (browserName == ‘Microsoft Edge‘ ? userAgent.match(/edge/[d.]+/gi)[0].replace(‘edge/‘, ‘‘) : (browserName == ‘Firefox‘ ? userAgent.match(/firefox/[d.]+/gi)[0].replace(‘firefox/‘, ‘‘) : (browserName == ‘Chrome‘ ? userAgent.match(/chrome/[d.]+/gi)[0].replace(‘chrome/‘, ‘‘) : (browserName == ‘Safari‘ ? userAgent.match(/safari/[d.]+/gi)[0].replace(‘safari/‘, ‘‘) : ‘Unknown‘)))); var browserEdition = browserVersion.split(‘.‘)[0]; var kernelName = userAgent.indexOf(‘trident‘) > -1 ? ‘Trident‘: userAgent.indexOf(‘edge‘) > -1 ? ‘Edge‘: (((userAgent.indexOf(‘applewebkit‘) > -1) ? ‘Webkit‘: ((userAgent.indexOf(‘gecko‘) > -1) ? ‘Gecko‘: ‘Unknown‘))); var kernelVersion = kernelName == ‘Trident‘ ? (userAgent.match(/Trident/[d.]+/gi)[0].replace(‘Trident/‘, ‘‘)) : (browserName == ‘Firefox‘ ? userAgent.match(/gecko/[d.]+/gi)[0].replace(‘gecko/‘, ‘‘) : ((browserName == ‘Chrome‘ || browserName == ‘Safari‘) ? userAgent.match(/applewebkit/[d.]+/gi)[0].replace(‘applewebkit/‘, ‘‘) : (browserName == ‘Microsoft Edge‘ ? userAgent.match(/edge/[d.]+/gi)[0].replace(‘edge/‘, ‘‘) : ‘Unknown‘))); var kernelEdition = kernelVersion.split(‘.‘)[0]; var osName = userAgent.indexOf(‘android‘) > -1 ? ‘Android‘: (userAgent.indexOf(‘ipad‘) > -1 ? ‘ios(iPad)‘: (userAgent.indexOf(‘iphone‘) > -1 ? ‘iOS(iPhone)‘: userAgent.indexOf(‘windows phone‘) > -1 ? ‘Windows Phone‘: (((platform == "Win32") || (platform == "Windows") ? ‘Microsoft Windows‘: (navigator.platform == "Mac68K") || ((navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel") ? ‘Apple Mac‘: ‘Unknown‘))))); var osEdition = osName == ‘Android‘ ? userAgent.match(/androids[d.]+/gi)[0].replace(‘android ‘, ‘‘) : ((osName == ‘iOS(iPad)‘ || osName == ‘iOS(iPhone)‘) ? userAgent.match(/oss[d_]+/gi)[0].replace(‘os ‘, ‘‘).replace(‘_‘, ‘.‘) : (osName == ‘Windows Phone‘ ? userAgent.match(/windowssphones[d.]+/gi)[0].replace(‘windows phone ‘, ‘‘).replace(‘_‘, ‘.‘) : (osName == ‘Microsoft Windows‘ ? (NT[userAgent.match(/windowssnts[d.]+/gi)[0].replace(‘windows nt ‘, ‘‘)] ? NT[userAgent.match(/windowssnts[d.]+/gi)[0].replace(‘windows nt ‘, ‘‘)] : ‘Unknown‘) : ‘Unknown‘))); return { browser: { name: browserName, edition: browserEdition, version: browserVersion }, kernel: { name: kernelName, edition: kernelEdition, version: kernelVersion }, os: { name: osName, edition: osEdition } }; }
以上是关于识别浏览器方法的主要内容,如果未能解决你的问题,请参考以下文章