• 来源于网络,用以备忘
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    function getBrowser()
    {
    //注意关键字大小写
    var ua_str = navigator.userAgent.toLowerCase(), ie_Tridents, trident, match_str, ie_aer_rv, browser_chi_Type;
    //判断IE 浏览器,
    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"
    browser_chi_Type = (match_str = ua_str.match(/edge\/([\d.]+)/)) ? "Edge" :
    //判断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;
    }
    //返回浏览器类型和版本号
    var verNum = trident && ie_Tridents[trident] ? ie_Tridents[trident] : match_str[1];
    var info=new Array();
    info.name=browser_chi_Type;
    info.version=verNum;
    return info;
    }
  • 调用方法
    1
    2
    console.log("浏览器:"+getBrowser().name);
    console.log("版本:"+getBrowser().version);