Class EventTarget
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The EventTarget interface is implemented by objects that can receive events and may have listeners for them.
In other words, any target of events implements the three methods associated with this interface.
[Value("EventTarget")]
public class EventTarget
- Inheritance
-
EventTarget
- Derived
- Inherited Members
Remarks
Element, and its children, as well as Document and Window, are the most common event targets,
but other objects can be event targets, too.
For example IDBRequest, AudioNode, and AudioContext are also event targets.
Many event targets (including elements, documents, and windows) also support registering event handlers via onevent properties and attributes.
-Event index
-Introduction to events
-Event interface
Constructors
EventTarget()
The EventTarget() constructor creates a new EventTarget object instance.
public EventTarget()
Remarks
NOTE
It is fairly rare to explicitly call this constructor. Most of the time, this constructor is used inside the constructor of an object extending the EventTarget interface, using the
superkeyword.
Methods
AddEventListener(string, EventListener?, Union26)
The addEventListener() method of the EventTarget interface
sets up a function that will be called whenever the specified event is delivered to the target.
[Value("addEventListener")]
public GlobalObject.Undefined AddEventListener(string type, EventListener? callback, Union26 options = default)
Parameters
typestringcallbackEventListeneroptionsUnion26
Returns
Remarks
Common targets are Element, or its children, Document, and Window,
but the target may be any object that supports events (such as IDBRequest).
NOTE
The
addEventListener()method is the recommended way to register an event listener. The benefits are as follows:
The method addEventListener() works by adding a function, or an object that implements a handleEvent() function, to the list of event listeners for the specified event type
on the EventTarget on which it's called. If the function or object is already in the list of event listeners for this target, the function or object is not added a second time.
NOTE
If a particular anonymous function is in the list of event listeners registered for a certain target, and then later in the code, an identical anonymous function is given in an
addEventListenercall, the second function will also be added to the list of event listeners for that target.Indeed, anonymous functions are not identical even if defined using
the same unchanging source-code called repeatedly, even if in a loop.Repeatedly defining the same unnamed function in such cases can be
problematic. (See Memory issues, below.)
If an event listener is added to an EventTarget from inside another listener —
that is, during the processing of the event —
that event will not trigger the new listener.
However, the new listener may be triggered during a later stage of event flow,
such as during the bubbling phase.
-RemoveEventListener(string, Action?, Union27)
-Creating and dispatching custom events
-More details on the use of this in event handlers
AddEventListener(string, Action?, Union26)
[To("FirstCharToLowerCase")]
public GlobalObject.Undefined AddEventListener(string type, Action? callback, Union26 options)
Parameters
Returns
AddEventListener(string, Action<Event>?, Union26)
[To("FirstCharToLowerCase")]
public GlobalObject.Undefined AddEventListener(string type, Action<Event>? callback, Union26 options)
Parameters
Returns
AddEventListener(string, Action<MouseEvent>?, Union26)
[To("FirstCharToLowerCase")]
public GlobalObject.Undefined AddEventListener(string type, Action<MouseEvent>? callback, Union26 options)
Parameters
typestringcallbackAction<MouseEvent>optionsUnion26
Returns
DispatchEvent(Event)
The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected
event listeners in the appropriate order. The normal event processing
rules (including the capturing and optional bubbling phase) also apply to events
dispatched manually with dispatchEvent().
[Value("dispatchEvent")]
public bool DispatchEvent(Event event_)
Parameters
event_Event
Returns
- bool
falseifeventis cancelable, and at least one of the event handlers which receivedeventcalled PreventDefault(). Otherwisetrue.
Remarks
Calling dispatchEvent() is the last step to firing an event. The event
should have already been created and initialized using an EventEvent constructor.
NOTE
When calling this method, the Target property is initialized to the current
EventTarget.
Unlike "native" events, which are fired by the browser and invoke event handlers
asynchronously via the event loop,dispatchEvent() invokes event handlers synchronously. All applicable event
handlers are called and return before dispatchEvent() returns.
RemoveEventListener(string, EventListener?, Union27)
The removeEventListener() method of the EventTarget interface
removes an event listener previously registered with AddEventListener(string, Action?, Union26) from the target.
The event listener to be removed is identified using a combination of the event type,
the event listener function itself, and various optional options that may affect the matching process;
see Matching event listeners for removal.
[Value("removeEventListener")]
public GlobalObject.Undefined RemoveEventListener(string type, EventListener? callback, Union27 options = default)
Parameters
typestringcallbackEventListeneroptionsUnion27
Returns
- GlobalObject.Undefined
None.
Remarks
Calling removeEventListener() with arguments that do not identify any
currently registered event listener on the EventTarget has no
effect.
If an event listener is removed from an EventTarget while another listener of the target is processing an event, it will not be triggered by the event. However, it can be reattached.
WARNING
If a listener is registered twice, one with the capture flag set and one without, you must remove each one separately. Removal of a capturing listener does not affect a non-capturing version of the same listener, and vice versa.
Event listeners can also be removed by passing an AbortSignal to an EventTargetaddEventListener and then later calling AbortControllerabort on the controller owning the signal.
RemoveEventListener(string, Action?, Union27)
[To("FirstCharToLowerCase")]
public GlobalObject.Undefined RemoveEventListener(string type, Action? callback, Union27 options)
Parameters
Returns
When(string, ObservableEventListenerOptions)
[Value("when")]
public Observable When(string type, ObservableEventListenerOptions options = null)
Parameters
typestringoptionsObservableEventListenerOptions