0

Can anyone point me to the documentation of the html Event object?


Bonus Reading

The only reason i know a global Event object exists is because it was mentioned in a Stackoverflow answer:

onclick="SomeEvent(this, event)"

function SomeEvent( el, event ) {
    var target = event.srcElement || event.target;

    if( el === target ) {
        // run your code
    }
}

Where it seems to have the properties:

  • srcElement
  • target

Googling around i found W3 School's page on the HTML DOM Event object, which lists the following properties:

  • bubbles: Returns whether or not an event is a bubbling event
  • cancelable: Returns whether or not an event can have its default action prevented
  • currentTarget: Returns the element whose event listeners triggered the event
  • eventPhase: Returns which phase of the event flow is currently being evaluated
  • target Returns the element that triggered the event
  • timeStamp Returns the time (in milliseconds relative to the epoch) at which the event was created - type Returns the name of the event

and methods:

  • initEvent(): Specifies the event type, whether or not the event can bubble, whether or not the event's default action can be prevented
  • preventDefault(): To cancel the event if it is cancelable, meaning that any default action normally taken by the implementation as a result of the event will not occur
  • stopPropagation(): To prevent further propagation of an event during event flow

It's missing srcElement, so it's safe to say it's not complete documentation.


Then there's Microsoft's page on the event object. It doesn't have any documentation; only mentioning the object. But it does mention that:

some properties might not have meaningful values during some events. For example, the fromElement and toElement properties

W3Schools page doesn't mention fromElement or toElement properties; so it's not complete.

The MSDN page references a link to W3C:

Standards information

Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5

A search of that page contains no mention of fromElement or toElement.

So can anyone point me to documentation of the html Event object?

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

2 Answers2

1

The MDN site provides very reliable information: https://developer.mozilla.org/en/DOM/event. Your event.toElement property is a non-standard Microsoft thing, a quick test showed undefined for the property in Firefox:

<body onclick="alert(event.srcElement)">Click

The correct property to use is target. Also note that event is not global, it is only a local variable. You are suggested to use addEventListener for adding DOM events as described in the MDN page.

quirksmode.org has nice tables on compatibility across browsers.

W3schools... well http://w3fools.com

Since you want to know more about the IE-specific srcElement property, consult Microsofts documention. From srcElement property:

Gets the element that the event was originally dispatched to. Compare to target.

Remarks

Note The srcElement property is provided for backward compatibility. Use the target property instead.

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • By *"global"* i simply meant it is globally available, as opposed to having to reference it as a property of something else. So that explains the *logic* of the answer in the linked SO question (`srcElement or target` to ensure we get *something*). Quirksmode is about 6 years out date (stops at IE6) – Ian Boyd May 17 '12 at 14:15
  • `event` in your `SomeEvent` example is not global, it's passed as argument. Although quirksmode.org lags behind, it's still useful if you want to lookup ancient information (such as non-standard IE-specific properties). – Lekensteyn May 17 '12 at 16:18
  • Is there any documentation anywhere on other objects that are passed as arguments? And unless *w3fools* has any information to add there is no need to link to a rant site. Based on your explanation that `srcElement` is ie-only, and it should be `target`, i see that the w3schools has the only `Event` object reference. – Ian Boyd May 18 '12 at 13:34
0

Javascript itself is only a concept. Its a blend of different dialects like Jscript & Gecko Javascript. Now different browsers treat Javascript differently, so there are plethora of different documentation floating around in the internet.

Now talking about the events:

  • IE has two events models
  • Mozilla and Safari two different ones
  • Opera has three

And then compatibility:

  • the IE DOM0 events model works differently than the DOM0 events model of every other browser
  • the IE proprietary attachEvent events model (also supported by Opera) is different from the W3C DOM2 events model
  • Mozilla, Safari and Opera support W3C DOM2 events
  • the Event object has a very different set of properties in IE as compared to the other three, independent of which event model you're talking about.

In fact, independent of which events model you're talking about, you will find differences between all four major browsers in various aspects. Thats why you are not finding a specific global documentation of event object.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
srijan
  • 1,504
  • 1
  • 13
  • 24