使用导航器对象/用户代理嗅探来检测IE版本的缺点 [英] Downsides of using the navigator object / user-agent sniffing for detecting IE versions

查看:244
本文介绍了使用导航器对象/用户代理嗅探来检测IE版本的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过发布jQuery 2.0 ,已经有很多关于如何识别用户是否正在使用支持它的IE版本(jQuery 2.0仅支持IE9及更高版本)的讨论。

With the release of jQuery 2.0, there has been a lot of talk about how to identify if the user is using an IE version which is supporting it or not (jQuery 2.0 only supports IE9 and later).

我的问题是为什么像这样的解决方案:

My question is why a solution like this:

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;

}());

比查看 navigator

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

推荐答案

特征检测(在这种情况下,检测对条件注释的支持)是可靠的,因为您知道给定浏览器的给定版本以某种方式实现了给定的特征。即使以后的版本删除了此功能(在这种情况下,条件注释在IE10中被删除),它不会改变先前版本实现它的方式。同样,对于以前未实现的功能,稍后在较新版本中引入的功能。

Feature detection (in this case, detecting support for conditional comments) is reliable as you know that a given version of a given browser has implemented a given feature in a certain way. Even if a later version removes this feature (in this case, conditional comments being dropped in IE10), it does not change how previous versions have implemented it. Likewise for a feature that wasn't implemented before and is later introduced in a newer version.

在使用标准时,功能检测通常与供应商无关。您正在查看某个功能是否受浏览器支持,无论它是什么类型的浏览器。这有助于促进浏览器之间的互操作性,同时避免不必要的歧视。

When working with standards, feature detection is also often vendor-independent. You're looking at whether a feature is supported by the browser, regardless of what sort of browser it is. This helps facilitate interoperability between browsers, while avoiding unnecessary discrimination.

另一方面,当使用用户代理字符串时,你取决于任意字符串的值其可以以各种方式被操纵,而不仅仅是由第三方或作者代码,甚至由用户代理本身。这个字符串很复杂,很难解析,通常代码试图解析它将失败在壮观的方式。这是什么让UA嗅闻这么不可靠。

On the other hand, when working with user agent strings, you're depending on the value of an arbitrary string that can be manipulated in all sorts of ways, not just by third parties or author code, but even by the user agent itself. This string is complex and difficult to parse, and often code that tries to parse it will fail in spectacular ways. That's what makes UA sniffing so unreliable.

modern.IE 更好地解释了缺点:


navigator.userAgent)检测

userAgent字符串是否指示特定功能(或错误)是否存在。为了使问题复杂化,解释userAgent的大部分代码都不正确。例如,一个浏览器嗅探库预计主要版本只有一个数字,所以它报告Firefox 15为Firefox 1和IE 10为IE 1!更可靠的是直接检测功能或问题,并使用作为代码分支的决策标准。我们建议将 Modernizr 作为实施功能检测的最简单方法。

Always prefer feature detection over browser (navigator.userAgent) detection.
The userAgent string is a poor indicator of whether a particular feature (or bug) is present. To compound the problem, much of the code that interprets userAgent does so incorrectly. For example, one browser-sniffing library expected the major version to be only a single digit, so it reported Firefox 15 as Firefox 1 and IE 10 as IE 1! It is more reliable to detect the feature or problem directly, and use that as the decision criteria for code branches. We recommend Modernizr as the easiest way to implement feature detection.

这篇关于使用导航器对象/用户代理嗅探来检测IE版本的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆