Web/API/EventTarget/dispatchEvent

From Get docs

Dispatches an Event at the specified EventTarget, (synchronously) invoking the affected EventListeners 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 the Event object to be dispatched.
  • target is used to initialize the Event.target and determine which event listeners to invoke.

Return Value

  • The return value is false if event is cancelable and at least one of the event handlers which received event called Event.preventDefault(). Otherwise it returns true.

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

Specification Status Comment
DOMThe definition of 'EventTarget.dispatchEvent()' in that specification. Living Standard Initial definition in the DOM 2 Events specification.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
dispatchEvent Chrome

Full support 4

Edge

Full support 12

Firefox

Full support 2

IE Full support 9


Full support 9


No support 6 — 11

Notes' Alternate Name'

Notes' Older versions of IE supported an equivalent, proprietary EventTarget.fireEvent() method. Alternate Name' Uses the non-standard name: fireEvent

Opera

Full support 9

Safari

Full support 3.2

WebView Android

Full support 4

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 3

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
See implementation notes.'
See implementation notes.
Uses a non-standard name.'
Uses a non-standard name.