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

浏览器useragent

时间:2019-03-05 15:38:34      阅读:510      评论:0      收藏:0      [点我收藏+]

标签:linux   引擎   ons   androi   iphone   pod   str   microsoft   code   

var ua = window.navigator.userAgent;

edge :Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134

Chrome:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36

fixfox:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0

ie11:Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko (兼容模式得出的)

ie10:Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)  (兼容模式得出的)

ie8:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; WOW64; Trident/4.0)

ie ua 参考:https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537503(v=vs.85)

Android 微信浏览器:Mozilla/5.0 (Linux; Android 8.0.0; STF-AL00 Build/HUAWEISTF-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/044504 Mobile Safari/537.36 MMWEBID/6992 MicroMessenger/7.0.3.1400(0x2700033A) Process/tools NetType/WIFI Language/zh_CN

Android QQ浏览器:Mozilla/5.0 (Linux; Android 8.0; STF-AL00 Build/HUAWEISTF-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044468 Mobile Safari/537.36 V1_AND_SQ_7.7.0_882_YYB_D QQ/7.7.0.3640 NetType/WIFI WebP/0.3.0 Pixel/1080

Android 华为浏览器:Mozilla/5.0 (Linux; U; Android 8.0.0zh-cn; STF-AL00 Build/HUAWEISTF-AL00) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.1 Mobile Safari/537.36

iOS 微信浏览器:Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16C101 MicroMessenger/7.0.0(0x17000024) NetType/WIFI Language/zh_CN

iOS Safari:Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1

    var client = function(){

        //呈现引擎
        var engine = {

            ie: 0,
            gecko: 0,
            webkit: 0,
            khtml: 0,
            opera: 0,

            //完整的版本号
            ver: null
        };

        //浏览器
        var browser = {

            //主要浏览器
            edge:0,
            ie: 0,
            firefox: 0,
            safari: 0,
            konq: 0,
            opera: 0,
            chrome: 0,

            //具体的版本号
            ver: null
        };

        //平台、设备和操作系统
        var system = {

            win: false,
            mac: false,
            x11: false,    //unix(包含各种Linux)

            //移动设备
            iphone: false,
            ipod: false,
            ipad: false,
            ios: false,
            android: false,
            nokiaN: false,
            winMobile: false,

            //游戏系统
            wii: false,
            ps: false
        };

        //检测呈现引擎和浏览器
        var ua = navigator.userAgent;
        if (window.opera){

            engine.ver = browser.ver = window.opera.version();
            engine.opera = browser.opera = parseFloat(engine.ver);

        } else if (/AppleWebKit\/(\S+)/.test(ua)){

            engine.ver = RegExp["$1"];
            engine.webkit = parseFloat(engine.ver);

            //确定是Edge, Chrome 还是 Safari
            if (/Edge\/(\S+)/.test(ua)){

                browser.ver = RegExp["$1"];
                browser.edge = parseFloat(browser.ver);
            }else if (/Chrome\/(\S+)/.test(ua)){

                browser.ver = RegExp["$1"];
                browser.chrome = parseFloat(browser.ver);
            } else if (/Version\/(\S+)/.test(ua)){

                browser.ver = RegExp["$1"];
                browser.safari = parseFloat(browser.ver);
            } else {

                //近似地确定版本号
                var safariVersion = 1;
                if (engine.webkit < 100){
                    safariVersion = 1;
                } else if (engine.webkit < 312){
                    safariVersion = 1.2;
                } else if (engine.webkit < 412){
                    safariVersion = 1.3;
                } else {
                    safariVersion = 2;
                }

                browser.safari = browser.ver = safariVersion;
            }

        } else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)){

            engine.ver = browser.ver = RegExp["$1"];
            engine.khtml = browser.konq = parseFloat(engine.ver);

        } else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)){

            engine.ver = RegExp["$1"];
            engine.gecko = parseFloat(engine.ver);

            //确定是不是 Firefox
            if (/Firefox\/(\S+)/.test(ua)){

                browser.ver = RegExp["$1"];
                browser.firefox = parseFloat(browser.ver);
            }

        } else if (/MSIE ([^;]+)/.test(ua)){

            engine.ver = browser.ver = RegExp["$1"];
            engine.ie = browser.ie = parseFloat(engine.ver);

        }else if(/Trident/.test(ua)){

            engine.ver = browser.ver = "11.0";
            engine.ie = browser.ie = 11;

        }

        //检测浏览器
        browser.ie = engine.ie;
        browser.opera = engine.opera;

        //检测平台
        var p = navigator.platform;
        system.win = p.indexOf("Win") == 0;
        system.mac = p.indexOf("Mac") == 0;
        system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);

        //检测 Windows 操作系统
        if (system.win){
            if (/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua)){
                if (RegExp["$1"] == "NT"){
                    switch(RegExp["$2"]){
                        case "5.0":
                            system.win = "2000";
                            break;

                        case "5.1":
                            system.win = "XP";
                            break;

                        case "6.0":
                            system.win = "Vista";
                            break;

                        case "6.1":
                            system.win = "7";
                            break;

                        case "6.2":
                            system.win = "8";
                            break;

                        case "6.3":
                            system.win = "8.1";
                            break;

                        case "10.0":
                            system.win = "10.0";
                            break;

                        default:
                            system.win = "NT";
                            break;
                    }

                } else if (RegExp["$1"] == "9x"){

                    system.win = "ME";

                } else {
                    system.win = RegExp["$1"];

                }
            }
        }

        //移动设备
        system.iphone = ua.indexOf("iPhone") > -1;
        system.ipod = ua.indexOf("iPod") > -1;
        system.ipad = ua.indexOf("iPad") > -1;
        system.nokiaN = ua.indexOf("NokiaN") > -1;

        //windows mobile
        if (system.win == "CE"){

            system.winMobile = system.win;

        } else if (system.win == "Ph"){

            if(/Windows Phone OS (\d+.\d+)/.test(ua)){;

                system.win = "Phone";
                system.winMobile = parseFloat(RegExp["$1"]);
            }
        }

        //检测 iOS 版本
        if (system.mac && ua.indexOf("Mobile") > -1){

            if (/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)){

                system.ios = parseFloat(RegExp.$1.replace("_", "."));

            } else {

                system.ios = 2; //不能真正检测出来,所以只能猜测
            }
        }

        //检测 Android 版本
        if (/Android (\d+\.\d+)/.test(ua)){
            system.android = parseFloat(RegExp.$1);

        }

        //游戏系统
        system.wii = ua.indexOf("Wii") > -1;
        system.ps = /playstation/i.test(ua);

        //返回这些对象
        return {
            engine:     engine,
            browser:    browser,
            system:     system
        };
    }();

    document.getElementById("client").innerHTML = JSON.stringify(client);
    console.log(client);

 

浏览器useragent

标签:linux   引擎   ons   androi   iphone   pod   str   microsoft   code   

原文地址:https://www.cnblogs.com/zhanglw456/p/10472266.html

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