js判断浏览器版本

浏览器类型判断

不考虑对 IE9 以下浏览器的判断

function getBrowserType(){
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var browser='unknown';
    if (userAgent.indexOf("IE")!=-1) {
        browser="IE";
    }else if(userAgent.indexOf('Firefox')!=-1){
        browser="Firefox";
    }else if(userAgent.indexOf('OPR')!=-1){
        browser="Opera";
    }else if(userAgent.indexOf('Chrome')!=-1){
        browser="Chrome";
    }else if(userAgent.indexOf('Safari')!=-1){
        browser="Safari";
    }else if(userAgent.indexOf('Trident')!=-1){
        browser='IE 11';
    }
    return browser;
}

测试

《js判断浏览器版本》

判断IE浏览器的版本

包含edge,ie11,10,9,8,7,6,5,4

// 获取IE版本
function IEVersion() {
    // 取得浏览器的userAgent字符串
    var userAgent = navigator.userAgent;
    // 判断是否为小于IE11的浏览器
    var isLessIE11 = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1;
    // 判断是否为IE的Edge浏览器
    var isEdge = userAgent.indexOf('Edge') > -1 && !isLessIE11;
    // 判断是否为IE11浏览器
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;
    if (isLessIE11) {
        var IEReg = new RegExp('MSIE (\\d+\\.\\d+);');
        // 正则表达式匹配浏览器的userAgent字符串中MSIE后的数字部分,,这一步不可省略!!!
        IEReg.test(userAgent);
        // 取正则表达式中第一个小括号里匹配到的值
        var IEVersionNum = parseFloat(RegExp['$1']);
        if (IEVersionNum === 7) {
            // IE7
            return 7
        } else if (IEVersionNum === 8) {
            // IE8
            return 8
        } else if (IEVersionNum === 9) {
            // IE9
            return 9
        } else if (IEVersionNum === 10) {
            // IE10
            return 10
        } else {
            // IE版本<7
            return 6
        }
    } else if (isEdge) {
        // edge
        return 'edge'
    } else if (isIE11) {
        // IE11
        return 11
    } else {
        // 不是ie浏览器
        return -1
    }
}

判断浏览器型号及版本号

function getBroswerAndVersion(){
    var os = navigator.platform;  
    var userAgent = navigator.userAgent;  
    var info = ""; 
    var tempArray = "";  
    //判断浏览器版本
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    var isEdge = userAgent.toLowerCase().indexOf("edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 = (userAgent.toLowerCase().indexOf("trident") > -1 && userAgent.indexOf("rv") > -1);  

    if (/[Ff]irefox(\/\d+\.\d+)/.test(userAgent)) {  
        tempArray = /([Ff]irefox)\/(\d+\.\d+)/.exec(userAgent);  
        info += tempArray[1] + tempArray[2];  
    } else if (isIE) {  

        var version = "";  
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");  
        reIE.test(userAgent);  
        var fIEVersion = parseFloat(RegExp["$1"]);  
        if (fIEVersion == 7)  
        { version = "IE7"; }  
        else if (fIEVersion == 8)  
        { version = "IE8"; }  
        else if (fIEVersion == 9)  
        { version = "IE9"; }  
        else if (fIEVersion == 10)  
        { version = "IE10"; }  
        else  
        { version = "0" }  

        info += version;  

    } else if (isEdge) {  
        info += "Edge";  
    } else if (isIE11) {  
        info += "IE11";  
    } else if (/[Cc]hrome\/\d+/.test(userAgent)) {  
        tempArray = /([Cc]hrome)\/(\d+)/.exec(userAgent);  
        info += tempArray[1] + tempArray[2];  
    } else if (/[Vv]ersion\/\d+\.\d+\.\d+(\.\d)* *[Ss]afari/.test(userAgent)) {  
        tempArray = /[Vv]ersion\/(\d+\.\d+\.\d+)(\.\d)* *([Ss]afari)/.exec(userAgent);  
        info += tempArray[3] + tempArray[1];  
    } else if (/[Oo]pera.+[Vv]ersion\/\d+\.\d+/.test(userAgent)) {  
        tempArray = /([Oo]pera).+[Vv]ersion\/(\d+)\.\d+/.exec(userAgent);  
        info += tempArray[1] + tempArray[2];  
    } else {  
        info += "unknown";  
    }  
    return info;
}

低于IE9版本提醒

参考:

https://blog.csdn.net/qq_35718410/article/details/90755204 

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *