EventTarget.dispatchEvent()
Dispatches an Event
at the specified EventTarget
, (synchronously) invoking the affected EventListener
s in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent()
.
Syntax
cancelled = !target.dispatchEvent(event)
Parameter
-
event
is theEvent
object to be dispatched. -
target
is used to initialize theEvent.target
and determine which event listeners to invoke.
Return Value
- The return value is
false
ifevent
is cancelable and at least one of the event handlers which receivedevent
calledEvent.preventDefault()
. Otherwise it returnstrue
.
The dispatchEvent()
method throws UNSPECIFIED_EVENT_TYPE_ERR
if the event's type was not specified by initializing the event before the method was called, or if the event's type is null
or an empty string.
Exceptions
Exceptions thrown by event handlers are reported as uncaught exceptions. The event handlers run on a nested callstack; they block the caller until they complete, but exceptions do not propagate to the caller.
Notes
Unlike "native" events, which are fired by the DOM and invoke event handlers asynchronously via the event loop, dispatchEvent()
invokes event handlers synchronously. All applicable event handlers will execute and return before the code continues on after the call to dispatchEvent()
.
dispatchEvent()
is the last step of the create-init-dispatch process, which is used for dispatching events into the implementation's event model. The event can be created using Event constructor.
See also the Event object reference.
Example
See Creating and triggering events.
Specifications
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 | |
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 |
© 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/dispatchEvent