EventTarget
EventTarget
is a DOM interface implemented by objects that can receive events and may have listeners for them.
Element
, Document
, and Window
are the most common event targets, but other objects can be event targets, too. For example XMLHttpRequest
, AudioNode
, AudioContext
, and others.
Many event targets (including elements, documents, and windows) also support setting event handlers via onevent
properties and attributes.
Constructor
EventTarget()
-
Creates a new
EventTarget
object instance.
Methods
EventTarget.addEventListener()
-
Registers an event handler of a specific event type on the
EventTarget
. EventTarget.removeEventListener()
-
Removes an event listener from the
EventTarget
. EventTarget.dispatchEvent()
-
Dispatches an event to this
EventTarget
.
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; } } }; EventTarget.prototype.dispatchEvent = function(event) { if (!(event.type in this.listeners)) { return true; } var stack = this.listeners[event.type].slice(); for (var i = 0, l = stack.length; i < l; i++) { stack[i].call(this, event); } return !event.defaultPrevented; };
Specifications
Specification |
---|
DOM Standard (DOM) # interface-eventtarget |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
EventTarget |
1 |
12 |
1 |
6 |
7 |
1
window.EventTarget did not exist on versions of Safari before 10.1. |
1 |
18 |
4 |
10.1 |
1
window.EventTarget did not exist on versions of Safari iOS before 10.3. |
1.0 |
EventTarget |
64 |
79 |
59 |
No |
51 |
14 |
64 |
64 |
59 |
47 |
14 |
9.0 |
addEventListener |
1
Before Chrome 49, the
type and listener parameters were optional. |
12 |
1 |
9
6-11
Older versions of IE supported an equivalent, proprietary
EventTarget.attachEvent() method. |
7 |
1 |
1
Before Chrome 49, the
type and listener parameters were optional. |
18
Before Chrome 49, the
type and listener parameters were optional. |
4 |
10.1 |
1 |
1.0
Before Samsung Internet 5.0, the
type and listener parameters were optional. |
dispatchEvent |
4 |
12 |
2 |
9
6-11
Older versions of IE supported an equivalent, proprietary
EventTarget.fireEvent() method. |
9 |
3.1 |
4 |
18 |
4 |
10.1 |
3 |
1.0 |
removeEventListener |
1 |
12 |
1 |
9
6-11
Older versions of IE supported an equivalent, proprietary
EventTarget.detachEvent() method. |
7 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
- Event reference - the events available in the platform.
- Introduction to events
-
Event
interface
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget