Let's say I have two elements on a page. One is a div, and the other is its child, an achor. Let's say that I've added an event to that anchor via anchor.addEvent('click', ...)
. If I set the div's .innerHTML = ''
, does the 'click
' event associated with the anchor get removed/disposed of/garbage collected?
views:
165answers:
3
+1
A:
It depends if you have still reference to "anchor" DOM instance. If so, it will stay in memory until all references are removed.
Test example:
var parent = new Element('div');
var child = new Element('div', {
events : {
click : function() {
alert('child clicked');
}
}
});
child.innerHTML = 'child content';
parent.appendChild(child);
document.body.appendChild(parent);
parent.innerHTML = 'parent content';
document.body.appendChild(child);
nemisj
2010-02-02 09:39:05
+1
A:
According to the MooTools API: destroy()
is a method that:
Empties an Element of all its children, removes and garbages the Element. Useful to clear memory before the pageUnload.
I suspect that what happens to anchors removed when their parent elements are removed using innerHTML = ''
is going to depend on the browser.
jQuery offers an empty()
method, I am guessing other libraries probably offer methods too. You can see a pretty good discussion of this topic in Removing an element from DOM.
artlung
2010-02-06 04:12:00
I use destroy() in MooTools when I want to completely get rid of something.
Shawn Steward
2010-02-06 04:15:32