Does remove a DOM object (in Javascript) will cause Memory leak if it has event attached?

Posted by seatoskyhk on Stack Overflow See other posts from Stack Overflow or by seatoskyhk
Published on 2010-06-08T09:12:30Z Indexed on 2010/06/08 9:22 UTC
Read the original article Hit count: 174

So, if in the javascript, I create a DOM object in the HTML page, and attach event listener to the DOM object, upon I remove the the DOM from HTML page, does the event listener still exist and causing memory leak?

  function myTest() {
     var obj = document.createElement('div');
     obj.addEventListener('click', function() {alert('whatever'); });
     var body = document.getElementById('body'); // assume there is a <div id='body'></div> already
     body.appendChild(obj);
  }

  // then after some user actions. I call this:
  function emptyPage() {
    var body = document.getElementById('body');
    body.innerHTML = '';  //empty it.
  }

So, the DOM object, <div> inside body is gone. But what about the eventlistener? I'm just afraid that it will cause memory leak.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom