I'm trying to understand the code of another programmer and there is a initEvents () method. Some articles tells that this is an outdated method, but I'm not sure. Could you help me? What it is? And is it really outdated?
I'm trying to understand the code of another programmer and there is a initEvents () method. Some articles tells that this is an outdated method, but I'm not sure. Could you help me? What it is? And is it really outdated?
Share Improve this question asked Jan 9, 2018 at 14:46 Bogdana PavlovaBogdana Pavlova 711 gold badge1 silver badge2 bronze badges 1- 3 read this you shouldn't use this... – user757095 Commented Jan 9, 2018 at 14:48
2 Answers
Reset to default 11This is indeed a deprecated method which will init the event before dispatching
, you should instead use the Event constructor which have a second argument for event initialisation and then dispatch the event as you like.
Something like this:
// create a look event that bubbles up and cannot be canceled
var evt = new Event("look", {"bubbles":true, "cancelable":false});
document.dispatchEvent(evt);
// event can be dispatched from any element, not only the document
myDiv.dispatchEvent(evt);
initEvents
has been deprecated.
Instead use this to Create Events :
const customEvent = new Event("CustomEventEmmiter", {"bubbles":true, "cancelable":false});
document.dispatchEvent(customEvent);
And listen to it like this :
window.addEventListener("CustomEventEmmiter", function(){
alert('CustomEventEmmiter TRIGGERED');
});
Check out more about Events here