码迷,mamicode.com
首页 > 其他好文 > 详细

识别浏览器方法

时间:2018-07-05 14:47:28      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:ros   trident   ica   dex   agent   name   rev   intel   explore   

获取浏览器的类型、版本及操作系统信息

敬上代码,抛砖引玉

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 = /(msie\s|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(/msie\s[\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(/android\s[\d.]+/gi)[0].replace(‘android ‘, ‘‘) : ((osName == ‘iOS(iPad)‘ || osName == ‘iOS(iPhone)‘) ? userAgent.match(/os\s[\d_]+/gi)[0].replace(‘os ‘, ‘‘).replace(‘_‘, ‘.‘) : (osName == ‘Windows Phone‘ ? userAgent.match(/windows\sphone\s[\d.]+/gi)[0].replace(‘windows phone ‘, ‘‘).replace(‘_‘, ‘.‘) : (osName == ‘Microsoft Windows‘ ? (NT[userAgent.match(/windows\snt\s[\d.]+/gi)[0].replace(‘windows nt ‘, ‘‘)] ? NT[userAgent.match(/windows\snt\s[\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
        }
    };
  }

 

识别浏览器方法

标签:ros   trident   ica   dex   agent   name   rev   intel   explore   

原文地址:https://www.cnblogs.com/zhou195/p/9267890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!