浏览器检测与特征检测 [英] Browser detection versus feature detection

查看:81
本文介绍了浏览器检测与特征检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打一个魔鬼的拥护者一会儿。我一直想知道为什么浏览器检测(而不是特征检测)被认为是一个扁平化作为一个坏的做法。如果我测试某个版本的某些浏览器并确认,某些功能行为是在一些可预测的方式,那么似乎决定做特殊情况下它似乎确定。推理是,它将在未来万无一失,因为这个部分浏览器版本不会改变。另一方面,如果我检测到一个DOM元素有一个函数X,它不一定意味着:

I am going to play a devil's advocate for a moment. I have been always wondering why browser detection (as opposed to feature detection) is considered to be a flat out as a bad practise. If I test a certain version of certain browser and confirm that, that certain functionality behaves is in some predictable way then it seems OK to decide to do special case it. The reasoning is that it will be in future foolproof, because this partial browser version is not going to change. On the other hand, if I detect that a DOM element has a function X, it does not necessarily mean that:






  • 更重要的是,即使在所有未来的浏览器中, b $ b

    我只是窥探jQuery源代码,他们通过在DOM中插入一个仔细构造的HTML代码片段来进行特征检测,然后他们检查它的某些特性。这是一个明智和坚实的方式,但我会说,这将是一个有点太重,如果我只是在我的小个人的JavaScript(没有jQuery)这样的东西。它们还具有实际上无限QA资源的优点。另一方面,你经常看到人们做的是他们检查函数X的存在,然后基于这个,他们假设函数将在所有具有此函数的浏览器中以某种方式表现。

    I just peeked into the jQuery source and they do feature detection by inserting a carefully constructed snippet of HTML into DOM and then they check it for certain features. It’s a sensible and solid way, but i would say that it would be a bit too heavy if i just did something like this in my little piece of personal JavaScript (without jQuery). They also have the advantage of practically infinite QA resources. On the other hand, what you often see people doing is that they check for the existence of function X, and then based on this, they assume the function will behave certain way in all browsers which have this function.

    我不是说什么,特征检测不是一件好事(如果使用正确),但我不知道为什么浏览器检测通常立即被解雇,即使它听起来逻辑。

    I’m not saying anything in the sense that feature detection is not a good thing (if used correctly), but I wonder why browser detection is usually immediately dismissed even if it sounds logical. I wonder whether it is another trendy thing to say.

    推荐答案

    在我看来,浏览器检测已经被广泛接受,因为 Resig几年前的这篇文章。然而,Resig的注释特定于库/框架代码,即将被其他[特定于域]的应用程序/站点消费的代码。

    It seems to me browser detection has been widely frowned upon since this post by Resig a couple of years ago. Resig's comments however were specific to libraries/framework code, i.e. code that will be consumed by other [domain-specific] applications/sites.

    我认为对于库/框架来说,特征检测毫无疑问是一个很好的契合。对于域特定的应用程序,但我不是那么确定浏览器检测是那么糟糕。它适合处理难以特征检测的已知浏览器特性,或适用于在其特性本身的实现中有错误的浏览器。适合浏览器检测的次数:

    I think feature detection is without question a good fit for libraries/frameworks. For domain-specific applications however I'm not so sure browser detection is that bad. It's suitable for working around known browser characteristics that are difficult to feature-detect, or for browsers that have bugs in their implementation of the feature itself. Times that browser detection is appropriate:


    • 不是跨浏览器并需要显示警告/对话框/ DifferentPage定制的网站/应用程序该客户的浏览器。

    • 支持哪些浏览器和版本的严格政策的银行或私人网站(以避免可能危及用户数据的已知安全漏洞)

    • 微优化:在以某种方式执行某些操作时,偶尔会有一个浏览器比其他浏览器快得多。

    • IE6中缺少png透明度

    • 许多显示/渲染

    • sites/applications that are not cross-browser and need to show a warning/dialog/DifferentPage tailoring to that client's browser. This is common in legacy applications.
    • Banks or private sites with strict policies on what browsers and versions are supported (to avoid known security exploits that may compromise user's data)
    • micro-optimizations: occasionally one browser is ridiculously faster than the others when performing some operation a certain way. It can be advantageous depending on your user base to branch on that particular browser/version.
    • Lack of png transparency in IE6
    • many display/rendering issues (read: IE css support) that are only witnessed in specific browser versions and you don't actually know what feature to test for.

    也就是说,有一些主要陷阱(可能由我们大多数人承担)避免当执行浏览器检测时。

    That said, there are some major pitfalls (probably committed by most of us) to avoid when doing browser detection.

    这篇关于浏览器检测与特征检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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