Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 787
Brock Adams Avatar asked Feb 03 '11 02:02

Brock Adams


1 Answers

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/

like image 199
gregers Avatar answered Sep 20 '22 12:09

gregers