6

From within a function like:

function eventHandler(e) { // ... } 

is there a reliable and efficient way to determine whether e is a DOM event?

5
  • How is eventHandler being called? Commented Jan 18, 2013 at 20:44
  • @TravisJ that isn't really relevant is it? The question is how can we test whether or not e is a DOM event. Commented Jan 18, 2013 at 20:49
  • @Shmiddty - I see, I think I had misunderstood the question. Commented Jan 18, 2013 at 20:52
  • Here is some interesting code that simulates events and uses jQuery. Perhaps you could use the list of properties and functions that it adds to the simulated events and check for those (or some of those). If its got enough of those functions and properties, you could consider it an event. (Sort of the "If it quacks like a duck, it is a duck" method of detection.) Commented Jan 18, 2013 at 21:07
  • Trevor, is your goal to prevent event spoofing? Commented Jan 18, 2013 at 21:19

3 Answers 3

3

I don't believe there is a reliable way to determine if a given object is NOT a DOM event.

  1. typeof e will always return 'object' for genuine Event objects, which is not helpful.
  2. Any property that you might check for on the object can exist in both a genuine Event object or any non-Event object.
  3. You might think that the prototype chain could be of use in determining this, but it has the same problem as #2 (can easily be replicated).
  4. The contructor property might seem promising, but one could do this:
function DummyEvent(){ this.constructor.toString = function(){ return "function MouseEvent() { [native code] }"; } } 

This ends up making console.log(e.constructor) print "function MouseEvent() { [native code] }"

So, is there a "reliable" way to tell if an object is an event? No.

Edit — Note that all of this is irrelevant if you want to prevent event spoofing as you can create real events easily.

var evt = new Event('click'); var evt2 = document.createEvent('MouseEvents'); evt2.initMouseEvent('click', ...); //etc 

Edit2 — I've created a test jsFiddle trying to find some way to distinguish objects, but I haven't found anything definite yet. Note that I didn't bother to specify properties on the DummyEvent because those can obviously easily be spoofed.

Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps if the event bubbles event.bubbles up through the DOM it's DOM event. But even if that statement is true its propagation might be stopped.

UPDATED :

OK the property you are looking for is e.target and for IE e.srcElement. Both properties return the HTML element the event took place on which defines it as DOM event.

However perhaps you should specify what you interpret as DOM event. Do you mean browser's native events, because this is DOM event too:

var zoo = jQuery('#zoo'); zoo.bind('moo'); zoo.trigger('moo'); 

It is binded to a DOM node

4 Comments

var spoof = { target: someDOMNode, srcElement: someDOMNode }; is not an Event object.
Then the function isn't an event handler either
A function is a function. You can call a function from anywhere (where it is in scope).
Where are you going, telling me that native functionality in js can be overridden or simulated , indeed that can be. Event object is an object after all from a native constructor.
-1

In jquery I created a testing function, something like this:

function isEvent(e) { try { e.PreventDefault(); return true; } catch (err) { return false; } 

}

and testing code:

var o = new Object(); var e = $.Event('click'); alert(isEvent(o)); alert(isEvent(e)); 

the former alert shows false, while the latter shows true. See fiddle.

UPDATE: It's safer to call something like preventDefault instead of trigger.

3 Comments

If you assign type, this test fails: e.type = 'click'; jsfiddle.net/eh6W7/1
Same problem... just add a PreventDefault method to the object and you won't get a reference error.
ur right, so there is no way to verify is by checking the object events and methods.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.