如何检查浏览器对功能/事件的支持? [英] How to check browser support for capabilities / events?

查看:98
本文介绍了如何检查浏览器对功能/事件的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我们使用浏览器嗅探来推断某些事件或功能是否可用。我知道浏览器嗅探已被弃用或避开,以支持功能嗅探。我想知道如何检查是否可以处理某个事件。

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.

DOMNodeInserted 为例。它受Chrome,FF和Safari的支持,但不受IE支持。如果此活动可用,我该如何嗅闻?有图书馆吗?你们怎么做适当的功能嗅探?

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?

推荐答案

你无法检测到突变事件,而且现代化不适用于此(因为人们会把事情吐出来作为事实上的答案)。

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:

var div = document.createElement('div'), supported = false;
div.addEventListener('DOMNodeInserted', function(){ supported = true; });
div.appendChild(div.cloneNode(true));

请注意,如果上面的代码是线性代码,那么它将无法正常工作,因为事件监听器是异步。总体思路是有效的,但也许最好用回调来实现。

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.

这篇关于如何检查浏览器对功能/事件的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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