3

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?

000
  • 26,951
  • 10
  • 71
  • 101
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203

3 Answers3

6

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.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • 2
    +1 for a good answer in general, but DOM mutation events aren't asynchronous in all browsers. As far as I'm aware the only situation they are async is in editable content in WebKit (although this may change). For example, try the following in Firefox 4: `var div = document.createElement('div'), a = "sync"; div.addEventListener('DOMNodeInserted', function(){ console.log("Inserted " + a); }, false); div.appendChild(document.createElement("br")); a = "async";` Also, your code doesn't work in WebKit: it seems you need to add the div into the document for the event to fire. – Tim Down Apr 19 '11 at 09:14
  • @Tim so we are doomed to do browser sniffing? – Kees C. Bakker Apr 19 '11 at 10:36
  • @vwolved "(...)perhaps best implemented with a callback". How do I detect an event that never fires? Setting a `Timeout` or something? – Kees C. Bakker Apr 19 '11 at 10:40
  • @Kees: No, browser sniffing is not required. This basic technique still works: you just need to temporarily add the test div to the document. Since there is a chance that DOM mutation events will become asynchronous in more browsers in future, you should allow for that, possibly with a timer. – Tim Down Apr 19 '11 at 10:45
  • 1
    @Kees: Here's an example feature test of `DOMAttrModified` that unfortunately relies on the event firing synchronously: https://github.com/dperini/nwevents/blob/ac33e52c1ed1c1c3a1bb1612384ca5b2f7a9b3ef/src/nwmatcher.js#L92 I got it from this article: http://perfectionkills.com/detecting-event-support-without-browser-sniffing/ – Tim Down Apr 19 '11 at 10:48
  • @Tim -- thanks for the feedback. I've done this before but didn't have time to test the code last night. I remember this being async somewhere, but you're right. I just tested it and it doesn't seem to be in any browser. –  Apr 19 '11 at 17:03
3

Checkout http://www.modernizr.com

hnprashanth
  • 791
  • 7
  • 23
-1

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

Andrew
  • 6,231
  • 2
  • 29
  • 37