Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1057858 of EventTarget

  • Revision slug: Web/API/EventTarget
  • Revision title: EventTarget
  • Revision id: 1057858
  • Created:
  • Creator: ViktorAksionov
  • Is current revision? No
  • Comment char of assigning is missing

Revision Content

{{ApiRef("DOM Events")}}

EventTarget is an interface implemented by objects that can receive events and may have listeners for them.

{{domxref("Element")}}, {{domxref("document")}}, and {{domxref("window")}} are the most common event targets, but other objects can be event targets too, for example {{domxref("XMLHttpRequest")}}, {{domxref("AudioNode")}}, {{domxref("AudioContext")}}, and others.

Many event targets (including elements, documents, and windows) also support setting event handlers via on... properties and attributes.

Methods

{{domxref("EventTarget.addEventListener()")}}
Register an event handler of a specific event type on the EventTarget.
{{domxref("EventTarget.removeEventListener()")}}
Removes an event listener from the EventTarget.
{{domxref("EventTarget.dispatchEvent()")}}
Dispatch an event to this EventTarget.

Additional methods for Mozilla chrome code

Mozilla extensions for use by JS-implemented event targets to implement on* properties. See also WebIDL bindings.

  • void setEventHandler(DOMString type, EventHandler handler) {{non-standard_inline}}
  • EventHandler getEventHandler(DOMString type) {{non-standard_inline}}

 

Example:

Simple implementation of EventTarget

var EventTarget = function() {
this.listeners = {};
};

EventTarget.prototype.listeners = null;
EventTarget.prototype.addEventListener = function(type, callback){
  if(!(type in this.listeners)) {
    this.listeners[type] = [];
  }
 this.listeners[type].push(callback);
};

EventTarget.prototype.removeEventListener = function(type, callback){
  if(!(type in this.listeners)) {
    return;
  }
  var stack = this.listeners[type];
  for(var i = 0, l = stack.length; i < l; i++){
     if(stack[i] === callback){
       stack.splice(i, 1);
       return this.removeEventListener(type, callback);
      }
     }
};

EventTarget.prototype.dispatchEvent = function(event){
  if(!(event.type in this.listeners)) {
    return;
  }
    var stack = this.listeners[event.type];
    event.target = this;
    for(var i = 0, l = stack.length; i < l; i++) {
        stack[i].call(this, event);
    }
};

{{ EmbedLiveSample('_Simple_implementation_of_EventTarget') }}

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#interface-eventtarget', 'EventTarget')}} {{Spec2('DOM WHATWG')}} No change.
{{SpecName('DOM3 Events', 'DOM3-Events.html#interface-EventTarget', 'EventTarget')}} {{Spec2('DOM3 Events')}} A few parameters are now optional (listener), or accepts the null value (useCapture).
{{SpecName('DOM2 Events', 'events.html#Events-EventTarget', 'EventTarget')}} {{Spec2('DOM2 Events')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 {{CompatGeckoDesktop("1")}} 9.0 7 1.0[1]
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 {{CompatGeckoMobile("1")}} 9.0 6.0 1.0

[1] window.EventTarget does not exist.

See Also

Revision Source

<p>{{ApiRef("DOM Events")}}</p>

<p><code>EventTarget</code> is an interface implemented by objects that can receive events and may have listeners for them.</p>

<p>{{domxref("Element")}}, {{domxref("document")}}, and {{domxref("window")}} are the most common event targets, but other objects can be event targets too, for example {{domxref("XMLHttpRequest")}}, {{domxref("AudioNode")}}, {{domxref("AudioContext")}}, and others.</p>

<p>Many event targets (including elements, documents, and windows) also support setting <a href="/en-US/docs/Web/Guide/DOM/Events/Event_handlers">event handlers</a> via <code>on...</code> properties and attributes.</p>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("EventTarget.addEventListener()")}}</dt>
 <dd>Register an event handler of a specific event type on the <code>EventTarget</code>.</dd>
 <dt>{{domxref("EventTarget.removeEventListener()")}}</dt>
 <dd>Removes an event listener from the <code>EventTarget</code>.</dd>
 <dt>{{domxref("EventTarget.dispatchEvent()")}}</dt>
 <dd>Dispatch an event to this <code>EventTarget</code>.</dd>
</dl>

<h3 id="Additional_methods_for_Mozilla_chrome_code">Additional methods for Mozilla chrome code</h3>

<p>Mozilla extensions for use by JS-implemented event targets to implement on* properties. See also <a href="/en-US/docs/Mozilla/WebIDL_bindings">WebIDL bindings</a>.</p>

<ul>
 <li>void <strong>setEventHandler</strong>(DOMString type, EventHandler handler) {{non-standard_inline}}</li>
 <li>EventHandler <strong>getEventHandler</strong>(DOMString type) {{non-standard_inline}}</li>
</ul>

<p>&nbsp;</p>

<h2 id="Example">Example:</h2>

<h3 id="_Simple_implementation_of_EventTarget" name="_Simple_implementation_of_EventTarget">Simple implementation of EventTarget</h3>

<pre class="brush: js">
<span style="background-color:rgb(255, 255, 255); font-family:open sans,arial,sans-serif; line-height:1.5">var EventTarget = function() {
</span>this.listeners = {};
};

<span style="background-color:rgb(255, 255, 255); font-family:open sans,arial,sans-serif; line-height:1.5">EventTarget.prototype.listeners = null;</span>
EventTarget.prototype.addEventListener = function(type, callback){
  if(!(type in this.listeners)) {
&nbsp;   this.listeners[type] = [];
  }
 this.listeners[type].push(callback);
};

EventTarget.prototype.removeEventListener = function(type, callback){
&nbsp; if(!(type in this.listeners)) {
&nbsp;   return;
&nbsp; }
&nbsp; var stack = this.listeners[type];
&nbsp; for(var i = 0, l = stack.length; i &lt; l; i++){
&nbsp;    if(stack[i] === callback){
&nbsp;      stack.splice(i, 1);
&nbsp;      return this.removeEventListener(type, callback);
&nbsp;     }
&nbsp;    }
};

EventTarget.prototype.dispatchEvent = function(event){
&nbsp; if(!(event.type in this.listeners)) {
&nbsp;   return;
&nbsp; }
&nbsp;   var stack = this.listeners[event.type];
&nbsp;   event.target = this;
&nbsp;   for(var i = 0, l = stack.length; i &lt; l; i++) {
&nbsp;       stack[i].call(this, event);
&nbsp;   }
};

<span style="background-color:rgb(255, 255, 255); font-family:open sans,arial,sans-serif; line-height:1.5">{{ EmbedLiveSample('_Simple_implementation_of_EventTarget') }}</span></pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#interface-eventtarget', 'EventTarget')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Events', 'DOM3-Events.html#interface-EventTarget', 'EventTarget')}}</td>
   <td>{{Spec2('DOM3 Events')}}</td>
   <td>A few parameters are now optional (<code>listener</code>), or accepts the <code>null</code> value (<code>useCapture</code>).</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Events', 'events.html#Events-EventTarget', 'EventTarget')}}</td>
   <td>{{Spec2('DOM2 Events')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1")}}</td>
   <td>9.0</td>
   <td>7</td>
   <td>1.0<sup>[1]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoMobile("1")}}</td>
   <td>9.0</td>
   <td>6.0</td>
   <td>1.0</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] <code>window.EventTarget</code> does not exist.</p>

<h2 id="See_Also">See Also</h2>

<ul>
 <li><a href="/en-US/docs/Web/Reference/Events">Event reference</a> - the events available in the platform.</li>
 <li><a href="/en-US/docs/Web/Guide/DOM/Events">Event developer guide</a></li>
 <li>{{domxref("Event")}} interface</li>
</ul>
Revert to this revision