What is the safest way to determine if a Javascript object is an event?
-
4@Tim Down: Because I need to make the difference between a random JS object, an HTML element and an event that's being sent as parameter to my function. – Tom Sep 23 '09 at 10:08
7 Answers
It's fairly good practice to probe possibly "unknown" objects for the properties and methods you expect to find.
So, assume you have got an event object, and probe it before acting on it, e.g.
if (event.target)
{
//looks like we're an event, hide the target
var e=$(event.target);
e.hide();
}
It's important to note that I'm NOT suggesting you test for 'target' to see if its an event: you're testing for target because you're about to use that property. What I'm driving at is that instead of trying to figure out whether an object is event, probe the object to see whether it is going to behave in the way you expect, then use those behaviours.
Code like this should degrade gracefully on browsers with different support, or let you take advantage of browser-specific extensions, e.g.
if (event.initKeyEvent)
{
//gecko 1.9+
event.initKeyEvent(...)
}

- 295,876
- 54
- 310
- 348
-
2Again, the event example will not work in IE, which has a srcElement property instead of target. – Tim Down Sep 22 '09 at 09:51
-
4You've misunderstood - you're not testing for 'target' to see if its an event, you're testing for target *because you're about to use that property*. What I'm driving at is that instead of trying to figure out whether an object is event, probe the object to see whether it is going to *behave* in the way you expect, then use those behaviours. – Paul Dixon Sep 22 '09 at 10:00
-
Fair enough. I don't think it really answers the original question, but then I can't see why you'd ever need to test if an object is an event anyway. – Tim Down Sep 22 '09 at 10:10
-
Now here's an interesting thought... I need only three properties of the Event object (if it really is an object) so I'll just check if it has all three an make sure their types are the ones I need. If the user is idiot enough to send me a fake object with usable values, it is entirely her fault. Thanks! – Tom Sep 23 '09 at 10:13
-
3I have to note that I came to this question for the opposite reason, I wanted to make sure an object is not an event. Taking Paul Dixon's advice is still possible, just check to see the object is what you are expecting. if($(object).is('div')){ do stuff...} . This will make sure an element is not an event object. – MrBrightside Sep 25 '12 at 20:06
-
Re Tim Down's question about testing for an event object. It can be an easy way to use a function both as an event handler and as a callable function with a parameter. If an event fires, the parameter is an event object, whereas when called, it is whatever the passed parameter is. I have used this to 'fire' a change event on a reset select element which was changing another field. – Patanjali Nov 17 '15 at 11:21
-
-
Beware: if (event.target) ... will provide a false positive in the case that the passed object is an anchor element eg. . To avoid that you could use: if (typeof event.target === 'object') ... – Ananda Masri Apr 03 '21 at 20:35
How about using instanceof
? As long as the event object was created using the constructor new Event()
, for instance:
var e = new Event('click');
e instanceof Event; // true
In case of the event parameter in event handlers, although its type is Object, it contains the native event as a property, so this could be used instead:
function myEventHandler(e) {
e.originalEvent instanceof Event; //true
}
Here it should be noted that the actual value may vary depending on the browser, specially when the event is a touch event, see here and references within. Not an issue in my case.
Old question, but apparently according to this, event.type
is cross-browser:
http://www.quirksmode.org/js/events_properties.html
RameshVel added this in an edit to his answer but it was heavily down-voted.
Of course the safest way is to follow the guidance of the accepted answer, but it just so happened that I want to discard the object if it is an event.

- 3,010
- 9
- 42
- 80
You could check if the object has an property originalEvent;
event.hasOwnProperty('originalEvent')
e.g:
// var event
var
isObject = typeof event ==='object', // is the given argument an object
isEvent = isObject ? event.hasOwnProperty('originalEvent') : false;
if(isEvent) {
// var `event` is an event!
} else {
// var event is NOT an event!
}

- 49
- 1
- 5
-
3originalEvent is a jQuery thing, right? That's worth noting. This kind of abstraction is why I used to love jQuery back in the day. – Brent Rittenhouse May 28 '19 at 04:04
This isEvent function checks the constructor for the unknown object, converts it to a string, and then searches that for the known event types:
function isEvent(o){
//grab the constructor for the unknown object
var c=o.constructor;
//convert constructor to string
var s=c.toString();
/* declare IE RegExp pattern to match for 'object [Event]' */
if(document.all){
//set regExp pattern for IE
var ptr=/\[object Event\]/;
}else{
/* declare FIREFOX regExp pattern to match for 'object [*Event]' since it has
several event types:
UIEvent
KeyboardEvent
MouseEvent
FocusEvent
WheelEvent
CompositionEvent
StorageEvent
CustomEvent (Requires Gecko 6.0)
MutationEvent
Both Custom and Mutation events are not recognized prior to Gecko 6.0,
so if you want to use these, adjust regExp accordingly */
var ptr=/\[object (Keyboard|Mouse|Focus|Wheel|Composition|Storage)Event\]/;
}
return ptr.test(s); }

- 51
- 3
I have the same concern. So I set out to prove, and I got closer to the solution.
function isEvent(a){
var txt, es=false;
txt = Object.prototype.toString.call(a).split('').reverse().join('');
es = (txt.indexOf("]tnevE") == 0)? true: false; // Firefox, Opera, Safari, Chrome
if(!es){
txt = a.constructor.toString().split('').reverse().join('');
es = (txt.indexOf("]tnevE") == 0)? true: false; // MSIE
}
return es;
}
I tested this feature in
- FF and Opera 12.0 on Ubuntu 11.64
- Safari 5.0 on Wine-Ubuntu
- G.Chrome 19 and MSIE 8 on WinXP
I hope this helps.
I don't know if there's a sure-fire way to do that, but I think your best shot is duck-typing.
Anyway, depending on the situation you could check if a given object has the expected properties that you want to use, just like Paul pointed out.

- 1
- 1

- 5,749
- 4
- 23
- 28