EventTarget: dispatchEvent() method
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available inWeb Workers.
ThedispatchEvent()
method of theEventTarget
sends anEvent
to the object, (synchronously) invoking the affectedevent listeners in the appropriate order. The normal event processingrules (including the capturing and optional bubbling phase) also apply to eventsdispatched manually withdispatchEvent()
.
CallingdispatchEvent()
is the last step tofiring an event. The eventshould have already been created and initialized using anEvent()
constructor.
Note:When calling this method, theEvent.target
property is initialized to the currentEventTarget
.
Unlike "native" events, which are fired by the browser and invoke event handlersasynchronously via theevent loop,dispatchEvent()
invokes event handlerssynchronously. All applicable eventhandlers are called and return beforedispatchEvent()
returns.
Syntax
dispatchEvent(event)
Parameters
event
The
Event
object to dispatch. ItsEvent.target
property will be set to the currentEventTarget
.
Return value
false
ifevent
is cancelable, and at least one of the event handlers which receivedevent
calledEvent.preventDefault()
. Otherwisetrue
.
Exceptions
InvalidStateError
DomException
Thrown if the event's type was not specified during event initialization.
Warning:Exceptions thrown by event handlers are reported as uncaught exceptions. The eventhandlers run on a nested callstack; they block the caller until they complete, butexceptions do not propagate to the caller.
Example
Specifications
Specification |
---|
DOM # ref-for-dom-eventtarget-dispatchevent③ |