如何使用JQuery.support检查浏览器是否为Firefox?

时间:2021-12-12 17:03:56

It seems that jQuery.browser is deprecated in the latest jQuery. The doc recommends that I use jQuery.support. Which of the support tests should I use to check if the current browser is Firefox?

似乎jQuery.browser在最新的jQuery中已被弃用。该文档建议我使用jQuery.support。我应该使用哪种支持测试来检查当前浏览器是否为Firefox?

1 个解决方案

#1


You are missing the point of the 'support' method. You don't check if user is using Firefox or not. You check whether the browser 'supports' whatever DOM manipulation you are trying to do. This is called 'feature detection' which is preferred compared to 'browser detection'(what you are trying to do).

您错过了“支持”方法的要点。您不检查用户是否使用Firefox。您检查浏览器是否“支持”您尝试进行的任何DOM操作。这称为“特征检测”,与“浏览器检测”(您正在尝试做的)相比,这是首选。

E.g. let's say you want verify whether the browser can render your CSS which contains 'opacity', so you use the jquery.support.opacity property.

例如。假设您要验证浏览器是否可以呈现包含“不透明度”的CSS,因此您使用jquery.support.opacity属性。

So why would you want to do 'feature detection'? Simple, you can write cross browser scripts which are future compatible.

那你为什么要做'特征检测'呢?很简单,您可以编写未来兼容的跨浏览器脚本。

Let's say that you check for 'Firefox' and apply some elaborate DOM/CSS effects. What if the next version of Firefox breaks the particular DOM/CSS feature you were relying on? You will have to go back and update your script. But if you used 'feature detection', your code will be future proof against such changes.

假设您检查“Firefox”并应用一些精心设计的DOM / CSS效果。如果下一版本的Firefox破坏了您所依赖的特定DOM / CSS功能,该怎么办?您将不得不返回并更新您的脚本。但是,如果您使用“特征检测”,您的代码将来可以抵御此类更改。

Now, I am not suggesting that you will be able to use 'feature detection' for all your requirements, but put some thought about whether you want to use feature detection or browser detection.

现在,我并不是说您可以根据所有要求使用“功能检测”,而是考虑是否要使用功能检测或浏览器检测。

Few more links worth reading:

更多链接值得一读:

http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting http://www.nczonline.net/blog/2006/11/16/browser-detection-versus-feature-detection/

#1


You are missing the point of the 'support' method. You don't check if user is using Firefox or not. You check whether the browser 'supports' whatever DOM manipulation you are trying to do. This is called 'feature detection' which is preferred compared to 'browser detection'(what you are trying to do).

您错过了“支持”方法的要点。您不检查用户是否使用Firefox。您检查浏览器是否“支持”您尝试进行的任何DOM操作。这称为“特征检测”,与“浏览器检测”(您正在尝试做的)相比,这是首选。

E.g. let's say you want verify whether the browser can render your CSS which contains 'opacity', so you use the jquery.support.opacity property.

例如。假设您要验证浏览器是否可以呈现包含“不透明度”的CSS,因此您使用jquery.support.opacity属性。

So why would you want to do 'feature detection'? Simple, you can write cross browser scripts which are future compatible.

那你为什么要做'特征检测'呢?很简单,您可以编写未来兼容的跨浏览器脚本。

Let's say that you check for 'Firefox' and apply some elaborate DOM/CSS effects. What if the next version of Firefox breaks the particular DOM/CSS feature you were relying on? You will have to go back and update your script. But if you used 'feature detection', your code will be future proof against such changes.

假设您检查“Firefox”并应用一些精心设计的DOM / CSS效果。如果下一版本的Firefox破坏了您所依赖的特定DOM / CSS功能,该怎么办?您将不得不返回并更新您的脚本。但是,如果您使用“特征检测”,您的代码将来可以抵御此类更改。

Now, I am not suggesting that you will be able to use 'feature detection' for all your requirements, but put some thought about whether you want to use feature detection or browser detection.

现在,我并不是说您可以根据所有要求使用“功能检测”,而是考虑是否要使用功能检测或浏览器检测。

Few more links worth reading:

更多链接值得一读:

http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting http://www.nczonline.net/blog/2006/11/16/browser-detection-versus-feature-detection/