views:

83

answers:

1

Similar to this question, my HTML looks like this:

<body id="body" onload="loader()">
</body>

I always assume, as this doc says, that onload is given no arguments. However, I named the argument, and did some deep inspection, and found that I got an object looking like this:

{originalTarget : DOM, 
preventCapture : function, 
target : DOM, 
cancelable : Bool, 
currentTarget : DOM, 
timeStamp : Int, 
bubbles : Bool, 
type : String, 
eventPhase : Int, 
preventDefault : function, 
initEvent : function, 
stopPropagation : function, 
CAPTURING_PHASE : Int, 
AT_TARGET : Int, 
BUBBLING_PHASE : Int, 
explicitOriginalTarget : DOM, 
preventBubble : function,
isTrusted : Bool, 
MOUSEDOWN : Int, 
MOUSEUP : Int, 
MOUSEOVER : Int, 
//... (more constants)
}

Anyone have any idea what that thing is, or what its classname might be?

+9  A: 

That appears to be the standard JavaScript DOM Event object. It describes the nature of the event that your function is handling.

UPDATE In response to comment discussion:

Different browsers supply the Event object in different ways:

  • IE never passes it as an argument to the function and instead uses the window.event property.
  • Firefox will pass it as the first argument.
  • Chrome seems to do both.
Annabelle
+1 Nice detective work :)
Jonathan Sampson
I believe any function called by any event handler (i.e. not just `body.onload`) gets passed this object as its first argument.
Paul D. Waite
@Paul: I thought there were differences between browser implementations and the way events were attached (using the traditional or the advanced event registration models). But I could be completely wrong (and too tired to look for it at the moment).
Marcel Korpel
@Jonathan: detective work? It's just in the specs. ;)
Marcel Korpel
@Marcel: Going to the spec in the first place is unfortunately a rarity today for young programmers. Nice work, Detective ;)
Jonathan Sampson
Nice update. I just found this comprehensive page: http://www.quirksmode.org/js/events_properties.html
Marcel Korpel
@Marcel: What specs?
Tim Down
@Marcel: sorry, you and Douglas are completely right. I’ve only really done event object stuff in jQuery, which always gives you an event object.
Paul D. Waite
@Tim: The specs of W3C, that Douglas linked to: http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event
Marcel Korpel
@Marcel: Oh I see, I thought you were talking about how the event object is passed into event handlers defined by HTML attributes, which I thought had no formal specification.
Tim Down
@Tim: The definition for the `EventListener` interface does specify an `Event` parameter for the `handleEvent` method. This would hardly be the first time browsers broke the spec though. ;)
Annabelle
@Douglas: There's a difference between how browsers handle events set via an HTML attribute and via an event handler property, and it was specifically the setting via an attribute that I thought had no formal spec. See this discussion: http://stackoverflow.com/questions/2196299/why-the-subtle-cross-browser-differences-in-event-object/2196356#2196356
Tim Down