javascriptcross-browserdom-eventsfeature-detectionmutation-events

Feature-detect: mutation-event availability in JavaScript?


How can my JavaScript detect if an event is available?

I'm aware of some great event-compatibility tables, but I need to use feature detection, not browser-sniffing plus a lookup table.

Specifically, my JS makes great use of the DOM mutation events (DOMNodeInserted and DOMSubtreeModified) -- which work great in all browsers except (of course) Internet Explorer.

So, how would I detect if a browser supports DOMNodeInserted?


Solution

  • If you just want to check if the browser supports mutation events in general, you can use this simple test:

    var hasMutationEvents = ("MutationEvent" in window);
    

    Here are the results from a bunch of popular browsers: http://www.browserscope.org/browse?category=usertest_agt1YS1wcm9maWxlcnINCxIEVGVzdBjEkNAPDA

    To run the browserscope test in another browser go here: http://jsbin.com/aqeton/4/