如果浏览器版本早于

时间:2018-01-12 12:01:24

标签: javascript jquery html browser

我有一个简单的问题 - 有没有办法做一些功能,检测用户是否使用较旧的浏览器版本,我可以自己设置?

如果有这样的声明,可以帮助我做的事情:

if(chromeVersion <= 48 || firefoxVersion <= 52 || ieVersion <= 10 || ...)
{//do something}

我需要这个,因为我使用的是现代插件,旧浏览器不支持它们,也无法预先修复它们。

度过美好的一天!

2 个答案:

答案 0 :(得分:3)

获取chrome版本:

function getChromeVersion () {     
    var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);

    return raw ? parseInt(raw[2], 10) : false;
}

对于多个浏览器

&#13;
&#13;
navigator.sayswho= (function(){
    var ua= navigator.userAgent, tem, 
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
})();

console.log(navigator.sayswho);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

尝试以下

var x = navigator.userAgent.match(/(Opera|Chrome|Safari|Firefox|Msie|trident(?=\/))\/?\s*(\d+)/);

console.log(x)
if (x[1] == "Chrome") {

  alert("Chrome")

  if (x[2] == "63") {

    alert("Chrome version is 63")

  }



}