嗅探 Firefox 4 及更高版本的方法
我正在编写一个 JavaScript 库,必须检查 Firefox 4 或更高版本。相信我,我需要这样做。
我计划使用以下嗅探代码:
if ('MozAppearance' in document.documentElement.style) {
//We have Mozilla
if (!!window.FormData) {
//We have Firefox4+
}
}
我对此感觉很好,因为(对我来说)似乎不太可能有人添加全局 FormData 方法。
假设我对与任何浏览器嗅探方法相关的常见风险感到满意,您能看出这有什么问题吗?
Firefox 中 window.FormData 的文档位于:
https://developer.mozilla.org/en /DOM/XMLHttpRequest/FormData
谢谢!
I am writing a JavaScript library that must check for Firefox 4 or higher. Trust me, I need to.
I'm planning to go with the following sniffing code:
if ('MozAppearance' in document.documentElement.style) {
//We have Mozilla
if (!!window.FormData) {
//We have Firefox4+
}
}
I feel ok about it because it seems rather unlikely (to me) that anyone will add a global FormData method.
Assuming I'm comfortable with the usual risks associated with any browser sniffing method, can you see any problems with this?
Documentation for window.FormData in Firefox is here:
https://developer.mozilla.org/en/DOM/XMLHttpRequest/FormData
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由您决定这是否是适合您的合理特征检测。如果您认为测试
FormData
是否存在就足够了;然后就这样吧。您也可以“增加”您检查的功能。您还可以查找
window.URL
,即 Firefox 4 中的新功能:因此,现在有人声明两者都具有的可能性甚至更低。如果您足够信任您的用户而不会更改他们,您也可以让用户代理参与其中。
That's up to you to decide if that is reasonable feature detection for you. If you believe that testing for the presence of
FormData
is enough; then go with that.You can "increase" the features you check for as well. You can also look for
window.URL
, a new feature in Firefox 4 as well:So now your odds that someone declared both are even lower. You could also get the user-agent involved as well if you trust your users enough to not change them.
window.history.pushState 是您可以检查的另一个。
我认为我们可以用 FF4+ 中出现但 FF3.x 中没有的功能来完成这一整天的工作。
window.history.pushState is another one you can check.
I think we could do this all day with features that appear in FF4+ but not FF3.x.
一旦他们删除前缀,它就会崩溃。
所有
-moz-
属性 标记为过时的事实证明.因此请记住,您必须使用
||
不断添加最新的 Mozilla 扩展。It will break the moment they drop the prefix.
All the
-moz-
properties tagged obsolete attest of that fact.So remember you will have to continually add recent Mozilla extensions using
||
.