Skip to content Skip to sidebar Skip to footer

Mouseevent.path Equivalent In Firefox & Safari

I'm using Polymer 1.0 and when there is a click on a button in Chrome a MouseEvent is generated. This MouseEvent object has a path property which is an ordered array of parent elem

Solution 1:

It's not available, but if you really would like to have this property, then you could extend the native prototype of the Event object like so:

if (!("path"inEvent.prototype))
Object.defineProperty(Event.prototype, "path", {
  get: function() {
    var path = [];
    var currentElem = this.target;
    while (currentElem) {
      path.push(currentElem);
      currentElem = currentElem.parentElement;
    }
    if (path.indexOf(window) === -1 && path.indexOf(document) === -1)
      path.push(document);
    if (path.indexOf(window) === -1)
      path.push(window);
    return path;
  }
});

However if I were you, I wouldn't extend the prototype - I would create a function like mentioned above instead.

Also I would change Event.prototype to MouseEvent.prototype if you want to cover only those types of events.

Post a Comment for "Mouseevent.path Equivalent In Firefox & Safari"