乐趣区

关于javascript:获取浏览器类型及版本ie谷歌火狐edge等

开发须要 –js 辨认局部浏览器类型及版本号,封装代码如下

function getBrowserInfo(){

let browserInfo = {};
let str = navigator.userAgent.toLowerCase();
let isIE = navigator.userAgent.indexOf("compatible") > -1 && navigator.userAgent.indexOf("MSIE") > -1; // 判断是否 IE<11 浏览器  
let isIE11 = navigator.userAgent.indexOf('Trident') > -1 && navigator.userAgent.indexOf("rv:11.0") > -1;
let requireStr =/(firefox|chrome|opera|version).*?([\d.]+)/;
if(isIE){let reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    let fIEVersion = parseFloat(RegExp["$1"]);
    if(fIEVersion == 7) {
        browserInfo.name ='ie';
        browserInfo.version ='ie7';
    } else if(fIEVersion == 8) {
        browserInfo.name ='ie';
        browserInfo.version ='ie78';
    } else if(fIEVersion == 9) {
        browserInfo.name ='ie';
        browserInfo.version ='ie9';
    } else if(fIEVersion == 10) {
        browserInfo.name ='ie';
        browserInfo.version ='ie10';
    } else {
        browserInfo.name ='ie';
        browserInfo.version ='<ie7';
    }   
}else if(isIE11){
    browserInfo.name ='ie';
    browserInfo.version ='ie11';
}else{let info = str.match(requireStr);
    browserInfo.name = info[1].replace(/version/, "'safari");//name--- 类型
    browserInfo.version = info[2];//version--- 版本
}

return browserInfo;
}

参考:
https://www.cnblogs.com/XCWeb…
https://www.cnblogs.com/xdp-g…

退出移动版