I would like to create a custom event emitter in my client-side programs. I am referencing this (sparse) documentation for EventTarget
My implementation atte
Here is how to do it using CustomEvent, cross-browser (fiddle):
// listen to event
window.addEventListener("say", function(e) { alert(e.detail.word); });
// create and dispatch the event
var event = document.createEvent("CustomEvent");
event.initCustomEvent('say', true, true,
{ "word": "Hello!" });
window.dispatchEvent(event);
You'd need to use window
or document
or any other existing DOM element to register listeneres and dispatch the event. EventTarget
is not a object, it's an interface. Try accessing EventTarget
in JavaScript console and you'll see that.