How to check browser support for capabilities / events?
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?
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.
Checkout http://www.modernizr.com
To answer the generic - how do I do feature sniffing - I use the jQuery.support object.
精彩评论