Are JavaScript event listeners cleaned up automatically? - Stack Overflow

After an element is removed from the DOM, will its event listeners automatically be unregistered and th

After an element is removed from the DOM, will its event listeners automatically be unregistered and their referenced resources (closures) cleaned up? If yes, will the answer change if some non-event listener code holds a reference to the element?

My specific concern relates to "pseudo-navigation" where the unload event is fired and most of the document is replaced (and of course many replacement elements also register event listeners), but there might be several pseudo-navigations before another full-page load occurs. So I'm wondering whether it's necessary to track all added event listeners and remove them manually when unload fires to avoid leaking any resources they reference.

(Notes: In my case, "use jQuery/[other js library] to handle it" isn't a valid solution. I'm interested in patibility with IE8+ and reasonably new versions of other browsers.)

After an element is removed from the DOM, will its event listeners automatically be unregistered and their referenced resources (closures) cleaned up? If yes, will the answer change if some non-event listener code holds a reference to the element?

My specific concern relates to "pseudo-navigation" where the unload event is fired and most of the document is replaced (and of course many replacement elements also register event listeners), but there might be several pseudo-navigations before another full-page load occurs. So I'm wondering whether it's necessary to track all added event listeners and remove them manually when unload fires to avoid leaking any resources they reference.

(Notes: In my case, "use jQuery/[other js library] to handle it" isn't a valid solution. I'm interested in patibility with IE8+ and reasonably new versions of other browsers.)

Share Improve this question edited Apr 17, 2021 at 21:39 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 17, 2015 at 1:40 ecraig12345ecraig12345 2,4672 gold badges20 silver badges27 bronze badges 2
  • possible duplicate of Will events automatically be unbound after the element was destroyed? – Jared Smith Commented Jan 17, 2015 at 1:42
  • The proposed duplicate is somewhat specifically about jQuery. I can't use jQuery. – ecraig12345 Commented Jan 17, 2015 at 1:46
Add a ment  | 

1 Answer 1

Reset to default 11

Event handlers will be removed and cleaned up when the DOM element is garbage collected as part of the garbage collection process in modern browsers. Note, this is different than when it is removed from the DOM because if any Javascript has references to the removed DOM element, it will be retained and will not be garbage collected.

Note: this is the intended functionality - some older browsers might have some bugs in this regard. Older versions of IE are the most prone to issues with garbage collection issues. Many other types of GC issues are documented with older versions of IE, though I'm not aware of any specific issues in the case you describe.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743730628a4497373.html

相关推荐

  • Are JavaScript event listeners cleaned up automatically? - Stack Overflow

    After an element is removed from the DOM, will its event listeners automatically be unregistered and th

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信