获取浏览器类型及版本(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 ='

return browserInfo;
}

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

你可能感兴趣的:(javascript前端)