Are DOM events destroyed after an element, they are connected, dies?

If I bind an event to a DOM element, will the event ever get destroyed if the element executes? By destroying an element, I mean removeChild (). Moving a node to another location using appendChild () leaves the event listener intact.

Basically, I'm interested in this because I want to know if I need to clean / tear.

+4
source share
1 answer

I think you mean the event handler here, right? If this is the case, then for the purpose of garbage collection it is important to be careful when using functions associated with elements through the "onfoo" attributes. IE has what constitutes a separate garbage collector for the DOM and JavaScript, and they know little about each other.

I believe that it’s enough to make sure that the β€œonfoo” attributes are set to null when the DOM elements are discarded. Thus, the JavaScript code will break the reference to the JavaScript memory allocated for the handlers, so the DOM garbage collector will not leak. Of course, this applies to any other random attributes that you might have added to the DOM elements too.

Although I do not like to suggest using the JavaScript framework for questions that are not tagged, and in fact I will not really make such a proposal here, but I will say that one of the things that the framework does for you (usually) is try to keep the DOM clean in this situation.

+3
source

Source: https://habr.com/ru/post/1342345/


All Articles