zoukankan      html  css  js  c++  java
  • 转载--JS根据浏览器的useAgent来判断浏览器的类型

    JS根据浏览器的useAgent来判断浏览器的类型

    userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。

    javascript语法:navigator.userAgent

    PHP语法:$_SERVER['HTTP_USER_AGENT']

    ASP语法:Request.ServerVariables("HTTP_USER_AGENT")

    ASP.NET语法:HttpContext.Current.Request.UserAgent

    JSP语法:request.getHeader("User-Agent")

    -------------------------------------------------------
    Chrome: (version: 50.0.2661.102 m)

      jsp:   userAgent :Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/50.0.2661.102 Safari/537.36

      js:   userAgent :  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/50.0.2661.102 Safari/537.36

    -------------------------------------------------------

    Firefox: (version: 47.0)

      jsp: userAgent : Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101Firefox/47.0

      js:   userAgent : Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101Firefox/47.0

    -------------------------------------------------------

    Safari: (version: 5.1.7)

      jsp: userAgent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko)Version/5.1.7Safari/534.57.2

      js:   userAgent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7Safari/534.57.2

    -------------------------------------------------------

    IE 8:  (version: 8.0.7601.17514, update versions: 0)

      jsp:  userAgent : Mozilla/4.0 (compatible;MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)

      js:   userAgent : Mozilla/4.0 (compatible;MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)

    -------------------------------------------------------

    IE 11:  (version: 11.0.9600.18321 update Versions: 11.0.31)

      jsp: userAgent : Mozilla/5.0 (Windows NT 6.3; WOW64;Trident/7.0; rv:11.0) like Gecko

      js:   userAgent : Mozilla/5.0 (Windows NT 6.3; WOW64;Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; rv:11.0) like Gecko

    -------------------------------------------------------

    Windows Edge:  (version: 25.10580.0.0)

      jsp: userAgent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586

      js:   userAgent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586

    -------------------------------------------------------

    [乐意黎 ]

    接下来,如何判断浏览器的类型呢 ?

    IE 浏览器 :

    TokenDescription
    Edge Windows Edge
    Trident/7.0 IE11
    Trident/6.0 Internet Explorer 10
    Trident/5.0 Internet Explorer 9
    Trident/4.0 Internet Explorer 8


    JS 代码: 

    //返回浏览器的类型: "ie", "firefox", "chrome", "opera", "safari", "unknow"

    // author: aerchi

    // site:www.aerchi.com

    // blog: http://blog.csdn.net/aerchi/article/details/51697592

    // email: aerchi@gmail.com

    // date: 2016-06-17

    // update: 2016-08-11 00:56:00

    // call like: getBrowser(1)
    // return like "chrome/50.0.2661.102"
    // call like: getBrowser()
    // return like "chrome"
    function getBrowser(getVersion)
    {
        //注意关键字大小写
        var ua_str = navigator.userAgent.toLowerCase(), ie_Tridents, trident, match_str, ie_aer_rv, browser_chi_Type;

        console.log("article url: http://blog.csdn.net/aerchi/article/details/51697592");

        //判断IE 浏览器, 
        //blog: http://blog.csdn.net/aerchi/article/details/51697592
        if("ActiveXObject" in self){
            // ie_aer_rv:  指示IE 的版本.
            // It can be affected by the current document mode of IE.
            ie_aer_rv= (match_str = ua_str.match(/msie ([d.]+)/)) ?match_str[1] :
                  (match_str = ua_str.match(/rv:([d.]+)/)) ?match_str[1] : 0;

            // ie: Indicate the really version of current IE browser.
            ie_Tridents = {"trident/7.0": 11, "trident/6.0": 10, "trident/5.0": 9, "trident/4.0": 8};
            //匹配 ie8, ie11, edge
            trident = (match_str = ua_str.match(/(trident/[d.]+|edge/[d.]+)/)) ?match_str[1] : undefined;
            browser_chi_Type = (ie_Tridents[trident] || ie_aer_rv) > 0 ? "ie" : undefined;
        }else{
            //判断 windows edge 浏览器
            // match_str[1]: 返回浏览器及版本号,如: "edge/13.10586"
            // match_str[1]: 返回版本号,如: "edge" 

            //若要返回 "edge" 请把下行的 "ie" 换成 "edge"。 注意引号及冒号是英文状态下输入的
            browser_chi_Type = (match_str = ua_str.match(/edge/([d.]+)/)) ? "ie" :
                        //判断firefox 浏览器
                          (match_str = ua_str.match(/firefox/([d.]+)/)) ? "firefox" : 
                        //判断chrome 浏览器
                          (match_str = ua_str.match(/chrome/([d.]+)/)) ? "chrome" : 
                        //判断opera 浏览器
                          (match_str = ua_str.match(/opera.([d.]+)/)) ? "opera" : 
                        //判断safari 浏览器
                          (match_str = ua_str.match(/version/([d.]+).*safari/)) ? "safari" : undefined;
        }


        console.log("author: aerchi, blog: http://blog.csdn.net/aerchi");    

        //返回浏览器类型和版本号

        var verNum, verStr;

        verNum = trident && ie_Tridents[trident] ? ie_Tridents[trident] : match_str[1];

        verStr = (getVersion != undefined) ? browser_chi_Type+"/"+verNum : browser_chi_Type;
        return verStr;
     }

    乐意黎原创, 严禁采集或用于个人网站。
    转载请注明作者及原文地址

    本文地址:  http://blog.csdn.net/aerchi/article/details/51697592

  • 相关阅读:
    mysql常用命令
    【转】Hibernate级联注解CascadeType参数详解
    【转】el表达式的判断符
    js中使用进行字符串传参
    【转】HTML5 jQuery图片上传前预览
    Win10家庭版安装Docker Desktop报错:Containers Windows Feature is not available
    采用反射机制,得出属性是否忽略
    Windows 我的常用命令
    数据相关需要注意,工作所遇场景
    引入 QueryDsl 开发步骤
  • 原文地址:https://www.cnblogs.com/lovehansong/p/7842717.html
Copyright © 2011-2022 走看看