查找浏览器类型&版本?

发布于 2024-11-17 06:59:31 字数 124 浏览 3 评论 0原文

任何人都知道一种良好且可靠的方法来找出类型和类型。使用 JavaScript/jQuery 安装在客户端的浏览器版本?

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

Anyone knows of a good and reliable way to find out type & version of a browser installed on client either using 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-11-24 06:59:31

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

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

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

无需重新发明轮子。

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.

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

Be sure to do feature detection instead 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.

弄潮 2024-11-24 06:59:31

方法一:
注意: 自 JQuery 1.3 起,不推荐使用 jQuery.browser

尝试一下:

<!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>

方法 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!');
}

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

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!');
}

谁对谁错谁最难过 2024-11-24 06:59:31

由于 $.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;

}());

Since $.browser is removed in jQuery 1.9, use this:

// ----------------------------------------------------------
// 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;

}());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文