How to check browser support for capabilities / events?

北城以北 提交于 2020-01-04 02:44:31

问题


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?


回答1:


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.




回答2:


Checkout http://www.modernizr.com




回答3:


To answer the generic - how do I do feature sniffing - I use the jQuery.support object.



来源:https://stackoverflow.com/questions/5713220/how-to-check-browser-support-for-capabilities-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!