dannym
dannym

Reputation: 267

detecting event capture support javascript

Is there a way to detect if a browser supports event capturing as well as event bubbling? I have checked http://modernizr.com/ but can't see any information in the documentation.

Am I correct in thinking that IE < 9 doesn't support event capture but in other browsers it should be ok?

For clarity I want to detect if the browser supports the event capture phase of the event DOM model as well as the event bubble phase.

Upvotes: 1

Views: 616

Answers (1)

James Allardice
James Allardice

Reputation: 166021

There may be better approaches to this, but this is the first thing I came up with. You would need to create an element, bind an event handler to it in capture phase, fire an event on it, and check the eventPhase property in the event handler:

var button = document.getElementById("example");

document.addEventListener("click", function (e) {
    console.log(e.eventPhase); // 1 === capture, 2 === target, 3 === bubble
}, true);

evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
button.dispatchEvent(evt);

You would probably want to add in all sorts of other checks (e.g. for dispatchEvent support) and you'd need to actually create the button element in the code (and insert it into the DOM, hidden).

Here's a fiddle containing the above code to get you started.

Upvotes: 1

Related Questions