如何检查浏览器对功能/事件的支持?
过去,我们使用浏览器嗅探来推断某些事件或功能是否可用。据我所知,浏览器嗅探已被“弃用”或“回避”,取而代之的是功能嗅探。我想知道如何检查是否可以处理某个事件。
以 DOMNodeInserted 为例。 Chrome、FF 和 Safari 支持,但 IE 不支持。我如何嗅探此事件是否可用?有图书馆吗?你们如何进行正确的功能嗅探?
In the past we used browser sniffing to infer if certain events or capabilities were available. I understand that browser sniffing has been 'deprecated' or 'shunned' in favor of feature sniffing. I would like to know how I can check if a certain event can be handled.
Take DOMNodeInserted
for example. It is supported by Chrome, FF and Safari, but not by IE. How can I sniff if this event is available? Is there a library present? How do you guys do proper feature sniffing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你无法检测突变事件,而 Modernizr 对此不起作用(因为人们会将其作为事实上的答案吐出来)。
“检测”对突变事件的支持的唯一方法是尝试并触发该事件。伪代码:
请注意,如果上面的代码是线性代码,则它不会按原样工作,因为事件侦听器是异步的。然而,总体想法是有效的,也许最好通过回调来实现。
You can't detect mutation events, and modernizr doesn't work for this (since people are going to spit that out as the defacto answer).
The only way to "detect" support for mutation events is to try and trigger the event. Pseudo code:
Note that the above code will not work as-is if it's in linear code because the event listener is async. The general idea is valid, however, perhaps best implemented with a callback.
查看 http://www.modernizr.com
Checkout http://www.modernizr.com
为了回答一般问题 - 我如何进行功能嗅探 - 我使用 jQuery.support 对象。
To answer the generic - how do I do feature sniffing - I use the jQuery.support object.