jQuery 查找浏览器类型和版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6505027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:58:10  来源:igfitidea点击:

Find Browser type & version?

jquerybrowser-detection

提问by Arjun

Anyone knows of a good and reliable way to find out type & version of a browser installed on client either using JavaScript/jQuery?

任何人都知道使用 JavaScript/jQuery 找出客户端上安装的浏览器的类型和版本的好方法和可靠方法吗?

Looks like jQuery has some built-in functions, but it is having problems in detecting Chrome. Any other reliable way of doing it?

看起来 jQuery 有一些内置函数,但是它在检测 Chrome 时遇到了问题。还有其他可靠的方法吗?

回答by kapa

If you want information about the browser that your visitor uses, and use it for statistics or displaying information to the user, you can use the jQuery Browser Plugin.

如果您需要有关访问者使用的浏览器的信息,并将其用于统计或向用户显示信息,您可以使用jQuery 浏览器插件

It gives you an object in javascript that contains all of the information about the browser being used.

它为您提供了一个 javascript 对象,其中包含有关正在使用的浏览器的所有信息。

Be sure to do feature detectioninstead of browser detection when you want to determine if a certain feature is available in a browser, apply bugfixes, etc.

当您想确定某个功能是否在浏览器中可用、应用错误修复等时,请务必进行功能检测而不是浏览器检测。

No need to reinvent the wheel.

无需重新发明轮子。

回答by Stephan

Approach 1:
Note:Since JQuery 1.3, jQuery.browser is deprecated

方法 1:
注意:从 JQuery 1.3 开始,不推荐使用 jQuery.browser

Try this :

尝试这个 :

<!DOCTYPE html>
<html>
<head>
<style>
 p { color:green; font-weight:bolder; margin:3px 0 0 10px; }
 div { color:blue; margin-left:20px; font-size:14px; }
 span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p>Browser info: (key : value)</p>

<script>
    jQuery.each(jQuery.browser, function(i, val) {
    $("<div>" + i + " : <span>" + val + "</span>")
               .appendTo( document.body );
    });</script>

</body>
</html>


Approach 2:

方法二:


// A quick solution without using regexp (to speed up a little).
var userAgent = navigator.userAgent.toString().toLowerCase();
if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1)) {
alert('We should be on Safari only!');
}

回答by Johan

Since $.browseris removed in jQuery 1.9, use this:

由于$.browser已在 jQuery 1.9 中删除,请使用:

// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
//     ie === undefined
// If you're in IE (>=5) then you can determine which version:
//     ie === 7; // IE7
// Thus, to detect IE:
//     if (ie) {}
// And to detect the version:
//     ie === 6 // IE6
//     ie > 7 // IE8, IE9 ...
//     ie < 9 // Anything less than IE9
// ----------------------------------------------------------

// UPDATE: Now using Live NodeList idea from @jdalton

var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());