Start a new topic

Global.document.addEventListener Example ?

Are there any examples of hooking events like:


Global.document.addEventListener


Intellisence shows 2 usage examples:


Global.document.addEventListener(dynamic type, Func<PointerEvent,dynamic> listener, [bool useCapture = false])


and


Global.document.addEventListener(string type, EventListener listener, [bool useCapture = false])


Thanks



 

var img = document.createElement<HTMLImageElement>("img");
img.addEventListener("mousemove", (PointerEvent pe) =>
{
    System.Console.WriteLine(pe.x + "," + pe.y);
    return true;
});


Using the EventListener interface is probably better choice. First of the Func<PointerEvent, dynamic> does NOT work with attachEvent (for IE Browsers). Also what if you are not listening

for a PointerEvent.


So i created a HtmlEventListener class:


public class HtmlEventListener : EventListener
{
private readonly Action<Event> Handler = null;
public HtmlEventListener(Action<Event> handler)
{
this.Handler = handler;
}

#region EventListener implementation
public void handleEvent(Event @event)
{
if (this.Handler != null) {
this.Handler(@event);
}
}
#endregion
}


Anyways i just use EventListener like so:


Element.addEventListener("change", new HtmlEventListener((Event e) => {
// TODO: Do something (optionally use Event action parameter)
}));


or


Element.attachEvent("change", new HtmlEventListener((Event e) => {
// TODO: Do something (optionally use Event action parameter)
}));

Login or Signup to post a comment