当前位置:

正文

最近在浏览许多博客站点的博主评论区时,发现很多用户的评论信息都带有当前用户的浏览器版本和操作系统信息,出于好奇心以及对技术的痴迷😁,在网上收集了许多了获取的方法,以下做一个归类总结。

 

注意:浏览器更新很快,有些代码可能会失效,需要你再次测试验证。

 

一、获取当前操作系统

   /**
     * 获取当前操作系统
    */
    getSys: function () {
        var OS = "";
        var OSArray = {};
        var UserAgent = navigator.userAgent.toLowerCase();
        OSArray.Windows = (navigator.platform == "Win32") || (navigator.platform == "Windows");
        OSArray.Mac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
            || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
        OSArray.iphone = UserAgent.indexOf("iPhone") > -1;
        OSArray.ipod = UserAgent.indexOf("iPod") > -1;
        OSArray.ipad = UserAgent.indexOf("iPad") > -1;
        OSArray.Android = UserAgent.indexOf("Android") > -1;
        for (var i in OSArray) {
            if (OSArray[i]) {
                OS = i;
            }
        }
        return OS;
    },

 

二、获取当前操作系统版本号

    /**
     * 获取当前操作系统版本号
     */
    getDigits: function () {
        var sUserAgent = navigator.userAgent.toLowerCase();
        var is64 = sUserAgent.indexOf("win64") > -1 || sUserAgent.indexOf("wow64") > -1;
        if (is64) {
            return "64位";
        }
        return "32位";
    },

 

三、获取浏览器及版本

    /**
     * 获取当前浏览器及版本
     */
    getBrowser: function () {  // 获取浏览器名
        var rMsie = /(msies|trident/7)([w.]+)/;
        var rTrident = /(trident)/([w.]+)/;
        var rEdge = /(chrome)/([w.]+)/;//IE
        var rFirefox = /(firefox)/([w.]+)/;  //火狐
        var rOpera = /(opera).+version/([w.]+)/;  //旧Opera
        var rNewOpera = /(opr)/(.+)/;  //新Opera 基于谷歌
        var rChrome = /(chrome)/([w.]+)/; //谷歌 
        var rUC = /(chrome)/([w.]+)/;//UC
        var rMaxthon = /(chrome)/([w.]+)/;//遨游
        var r2345 = /(chrome)/([w.]+)/;//2345
        var rQQ = /(chrome)/([w.]+)/;//QQ
        //var rMetasr =  /(metasr)/([w.]+)/;//搜狗
        var rSafari = /version/([w.]+).*(safari)/;
        var ua = navigator.userAgent.toLowerCase();
        var matchBS, matchBS2;
        //IE 低版
        matchBS = rMsie.exec(ua);
        if (matchBS != null) {
            matchBS2 = rTrident.exec(ua);
            if (matchBS2 != null) {
                switch (matchBS2[2]) {
                    case "4.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 8"  //内核版本号
                        };
                        break;
                    case "5.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 9"
                        };
                        break;
                    case "6.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 10"
                        };
                        break;
                    case "7.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 11"
                        };
                        break;
                    default:
                        return {
                            browser:
                                "Microsoft IE",
                            version: "Undefined"
                        };
                }
            } else {
                return {
                    browser: "Microsoft IE",
                    version: "IE:" + matchBS[2] || "0"
                };
            }
        }
        //IE最新版
        matchBS = rEdge.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "Microsoft Edge",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //UC浏览器					  
        matchBS = rUC.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "UC",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //火狐浏览器
        matchBS = rFirefox.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "火狐",
                version: "Firefox/" + matchBS[2] || "0"
            };
        }
        //Oper浏览器					 
        matchBS = rOpera.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "Opera",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //遨游
        matchBS = rMaxthon.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "遨游",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //2345浏览器					  
        matchBS = r2345.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "2345",
                version: "Chrome/ " + matchBS[2] || "0"
            };
        }
        //QQ浏览器					  
        matchBS = rQQ.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "QQ",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //Safari(苹果)浏览器
        matchBS = rSafari.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent)) && (!(window.chrome)) && (!(window.opera))) {
            return {
                browser: "Safari",
                version: "Safari/" + matchBS[1] || "0"
            };
        }
        //谷歌浏览器
        matchBS = rChrome.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            matchBS2 = rNewOpera.exec(ua);
            if (matchBS2 == null) {
                return {
                    browser: "谷歌",
                    version: "Chrome/" + matchBS[2] || "0"
                };
            } else {
                return {
                    browser: "Opera",
                    version: "opr/" + matchBS2[2] || "0"
                };
            }
        }
    }

 

以上的方法都是分开获取的,下面做了一个封装,统一调用

const BrowserMatch = {
    init: function () {
        this.browser = this.getBrowser().browser || "未知浏览器";  //获取浏览器名
        this.version = this.getBrowser().version || "未知浏览器版本号";  //获取浏览器版本
        this.OS = this.getSys() + " " + this.getDigits() || "未知操作系统"; //系统版本号 
    },
    /**
     * 获取当前操作系统
    */
    getSys: function () {
        var OS = "";
        var OSArray = {};
        var UserAgent = navigator.userAgent.toLowerCase();
        OSArray.Windows = (navigator.platform == "Win32") || (navigator.platform == "Windows");
        OSArray.Mac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
            || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
        OSArray.iphone = UserAgent.indexOf("iPhone") > -1;
        OSArray.ipod = UserAgent.indexOf("iPod") > -1;
        OSArray.ipad = UserAgent.indexOf("iPad") > -1;
        OSArray.Android = UserAgent.indexOf("Android") > -1;
        for (var i in OSArray) {
            if (OSArray[i]) {
                OS = i;
            }
        }
        return OS;
    },
    /**
     * 获取当前操作系统版本号
     */
    getDigits: function () {
        var sUserAgent = navigator.userAgent.toLowerCase();
        var is64 = sUserAgent.indexOf("win64") > -1 || sUserAgent.indexOf("wow64") > -1;
        if (is64) {
            return "64位";
        }
        return "32位";
    },

    /**
     * 获取当前浏览器及版本
     */
    getBrowser: function () {  // 获取浏览器名
        var rMsie = /(msies|trident/7)([w.]+)/;
        var rTrident = /(trident)/([w.]+)/;
        var rEdge = /(chrome)/([w.]+)/;//IE
        var rFirefox = /(firefox)/([w.]+)/;  //火狐
        var rOpera = /(opera).+version/([w.]+)/;  //旧Opera
        var rNewOpera = /(opr)/(.+)/;  //新Opera 基于谷歌
        var rChrome = /(chrome)/([w.]+)/; //谷歌 
        var rUC = /(chrome)/([w.]+)/;//UC
        var rMaxthon = /(chrome)/([w.]+)/;//遨游
        var r2345 = /(chrome)/([w.]+)/;//2345
        var rQQ = /(chrome)/([w.]+)/;//QQ
        //var rMetasr =  /(metasr)/([w.]+)/;//搜狗
        var rSafari = /version/([w.]+).*(safari)/;
        var ua = navigator.userAgent.toLowerCase();
        var matchBS, matchBS2;
        //IE 低版
        matchBS = rMsie.exec(ua);
        if (matchBS != null) {
            matchBS2 = rTrident.exec(ua);
            if (matchBS2 != null) {
                switch (matchBS2[2]) {
                    case "4.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 8"  //内核版本号
                        };
                        break;
                    case "5.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 9"
                        };
                        break;
                    case "6.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 10"
                        };
                        break;
                    case "7.0":
                        return {
                            browser:
                                "Microsoft IE",
                            version: "IE: 11"
                        };
                        break;
                    default:
                        return {
                            browser:
                                "Microsoft IE",
                            version: "Undefined"
                        };
                }
            } else {
                return {
                    browser: "Microsoft IE",
                    version: "IE:" + matchBS[2] || "0"
                };
            }
        }
        //IE最新版
        matchBS = rEdge.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "Microsoft Edge",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //UC浏览器					  
        matchBS = rUC.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "UC",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //火狐浏览器
        matchBS = rFirefox.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "火狐",
                version: "Firefox/" + matchBS[2] || "0"
            };
        }
        //Oper浏览器					 
        matchBS = rOpera.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "Opera",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //遨游
        matchBS = rMaxthon.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "遨游",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //2345浏览器					  
        matchBS = r2345.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "2345",
                version: "Chrome/ " + matchBS[2] || "0"
            };
        }
        //QQ浏览器					  
        matchBS = rQQ.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            return {
                browser: "QQ",
                version: "Chrome/" + matchBS[2] || "0"
            };
        }
        //Safari(苹果)浏览器
        matchBS = rSafari.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent)) && (!(window.chrome)) && (!(window.opera))) {
            return {
                browser: "Safari",
                version: "Safari/" + matchBS[1] || "0"
            };
        }
        //谷歌浏览器
        matchBS = rChrome.exec(ua);
        if ((matchBS != null) && (!(window.attachEvent))) {
            matchBS2 = rNewOpera.exec(ua);
            if (matchBS2 == null) {
                return {
                    browser: "谷歌",
                    version: "Chrome/" + matchBS[2] || "0"
                };
            } else {
                return {
                    browser: "Opera",
                    version: "opr/" + matchBS2[2] || "0"
                };
            }
        }
    }
}

 

参考:https://blog.csdn.net/u012762221/article/details/80419331

 

创作不易,感谢您对作者的支持!~( ̄▽ ̄)~
文章评论
评论列表 (有 0 条评论)
还没有评论,来说两句吧......