{{APIRef("DOM Events")}}
Dispatches an {{domxref("Event")}} at the specified {{domxref("EventTarget")}}, invoking the affected {{domxref("EventListener")}}s in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) apply to events dispatched manually with dispatchEvent()
.
Syntax
cancelled = !target.dispatchEvent(event)
event
is the {{domxref("Event")}} object to be dispatched.target
is used to initialize the {{domxref("Event", "", "target")}} and determine which event listeners to invoke.- The return value is
false
if at least one of the event handlers which handled this event called {{domxref("Event.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 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
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 document.createEvent method and initialized using initEvent or other, more specific, initialization methods, such as initMouseEvent or initUIEvent.
See also the Event object reference.
Example
See Creating and triggering events.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG', '#dom-eventtarget-dispatchevent', 'EventTarget.dispatchEvent()')}} | {{ Spec2('DOM WHATWG') }} | |
{{SpecName('DOM4', '#dom-eventtarget-dispatchevent', 'EventTarget.dispatchEvent()')}} | {{ Spec2('DOM4') }} | |
{{SpecName('DOM2 Events', '#Events-EventTarget-dispatchEvent', 'EventTarget.dispatchEvent()')}} | {{ Spec2('DOM2 Events') }} | Initial definition. |
Browser compatibility
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 26 (probably earlier) | 2 | 9 | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |