mozilla

Revision 401015 of DOM event handlers

  • Revision slug: Web/Guide/DOM/Events/Event_handlers
  • Revision title: DOM event handlers
  • Revision id: 401015
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 1 words addedDOM/DOM_event_handlers Web/Guide/DOM/Events/Event_handlers

Revision Content

Many DOM objects generate events that you can receive and handle in order to be aware of interesting things that happen in the browser. These can include user interactions, user interface changes (such as windows opening and closing), or progress of content loading, among other things.

Notable objects that generate events

This is a list of some of the more interesting objects that generate events. It's not a definitive list.

  • {{ domxref("window") }}
  • {{ domxref("document") }}
  • {{ domxref("element") }}

{{ h1_gecko_minversion("Event handler changes in Firefox 9", "9.0") }}

In order to better match the specifications, and improve cross-browser compatibility, the way event handlers were implemented at a fundamental level changed in Gecko 9.0 {{ geckoRelease("9.0") }}.

Specifically, in the past, event handlers were not correctly implemented as standard IDL attributes. In Gecko 9.0, this was changed. Because of this, certain behaviors of event handlers in Gecko have changed. In particular, they now behave in all the ways standard IDL attributes behave. In most cases, this shouldn't affect web or add-on content at all; however, there are a few specific things to watch out for.

Detecting the presence of event handler properties

You can now detect the presence of an event handler property (that is, for example, onload), using the JavaScript in operator. For example:

if ("onsomenewfeature" in window) {
  /* do something amazing */
}

Event handlers and prototypes

You can't set or access the values of any IDL-defined attributes on DOM prototype objects; that means you can't, for example, change Window.prototype.onload anymore. In the past, event handlers (onload, etc) weren't really implemented as IDL attributes in Gecko, so you were able to do this for those. Now you can't. This improves compatibility.

See also

Revision Source

<p>Many DOM objects generate events that you can receive and handle in order to be aware of interesting things that happen in the browser. These can include user interactions, user interface changes (such as windows opening and closing), or progress of content loading, among other things.</p>
<h2 id="Notable_objects_that_generate_events">Notable objects that generate events</h2>
<p>This is a list of some of the more interesting objects that generate events. It's not a definitive list.</p>
<ul> <li>{{ domxref("window") }}</li> <li>{{ domxref("document") }}</li> <li>{{ domxref("element") }}</li>
</ul>
<p>{{ h1_gecko_minversion("Event handler changes in Firefox 9", "9.0") }}</p>
<p>In order to better match the specifications, and improve cross-browser compatibility, the way event handlers were implemented at a fundamental level changed in Gecko 9.0 {{ geckoRelease("9.0") }}.</p>
<p>Specifically, in the past, event handlers were not correctly implemented as standard IDL attributes. In Gecko 9.0, this was changed. Because of this, certain behaviors of event handlers in Gecko have changed. In particular, they now behave in all the ways standard IDL attributes behave. In most cases, this shouldn't affect web or add-on content at all; however, there are a few specific things to watch out for.</p>
<h3 id="Detecting_the_presence_of_event_handler_properties">Detecting the presence of event handler properties</h3>
<p>You can now detect the presence of an event handler property (that is, for example, <code>onload</code>), using the JavaScript <a href="/en/JavaScript/Reference/Operators/in" title="en/JavaScript/Reference/Operators/in"><code>in</code></a> operator. For example:</p>
<pre class="brush: js">if ("onsomenewfeature" in window) {
  /* do something amazing */
}
</pre>
<h3 id="Event_handlers_and_prototypes">Event handlers and prototypes</h3>
<p>You can't set or access the values of any IDL-defined attributes on DOM prototype objects; that means you can't, for example, change <code>Window.prototype.onload</code> anymore. In the past, event handlers (<code>onload</code>, etc) weren't really implemented as IDL attributes in Gecko, so you were able to do this for those. Now you can't. This improves compatibility.</p><h2 id="See_also">See also</h2>
<ul> <li><a href="/en/DOM/DOM_event_reference" title="DOM event reference">DOM event reference</a></li>
</ul>
Revert to this revision