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

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

问题描述

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


  1. 此函数可以所有浏览器中的方式相同,

  2. 更重要的是,即使在所有未来的浏览器中也可以使用相同的方式。

我只是看着jQuery的源代码,他们通过在DOM中插入一个精心构造的HTML片段来进行特征检测,然后检查它们的某些功能。这是一个明智和坚实的方式,但我会说,如果我只是在我的小小的个人JavaScript(没有jQuery)中做了这样的事情,这将会有点太重了。它们也具有几乎无限QA资源的优势。另一方面,你经常看到人们所做的是检查函数X的存在,然后基于这一点,他们认为在所有具有这个功能的浏览器中,函数将以某种方式行为。



在这个意义上说,功能检测不是一件好事(如果使用正确),我并不在说任何东西,但是我不知道为什么浏览器检测通常立即被解雇,即使它听起来很合乎逻辑。我想知道这是否是另一个时髦的事情。

解决方案

看来,我的浏览器检测已经被广泛的皱眉了,因为这篇文章。然而,Resig的意见是针对图书馆/框架代码,即其他[特定于域的]应用程序/网站将消费的代码。



我认为功能检测对于库/框架来说是一个很好的适合。对于域特定的应用程序,但我不太确定浏览器检测是不好的。它适用于操作难以识别的已知浏览器特性,或适用于在功能本身实现中具有错误的浏览器。浏览器检测适合的时间:




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

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

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

  • IE6中缺少png透明度

  • 许多显示/渲染



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


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:

  1. This function works the same way in all browsers, and
  2. More crucially, it will work the same way even in all future browsers.

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.

解决方案

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:

  • 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天全站免登陆