From within a function like:
function eventHandler(e) {
// ...
}
is there a reliable and efficient way to determine whether e is a DOM event?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t believe there is a reliable way to determine if a given object is NOT a DOM event.
typeof ewill always return'object'for genuine Event objects, which is not helpful.contructorproperty might seem promising, but one could do this: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.
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
DummyEventbecause those can obviously easily be spoofed.