Mostly, these issues are caused by lingering references to the removed
DOM nodes (lookup tables, etc). In this case, since you use MochiKit,
you might want to look at the MochKit.Signal.disconnectAll().
It might also be helpful for some older browsers to tear down the DOM tree.
Here's a helper function that I've used to achieve these two aims:
/**
* Destroys a widget or a DOM node. This function will remove the DOM
* node from the tree, disconnect all signals and call all widget
* destructor functions. The same procedure will also be applied
* recursively to all child nodes. Once destroyed, all references to
* the widget object should be cleared in order for the browser to
* be able to reclaim the memory used.
*
* @param {Widget/Node/Array} node the (widget) DOM node or list
*
* @static
*/
MochiKit.Widget.destroyWidget = function (node) {
if (node.nodeType != null) {
if (typeof(node.destroy) == "function") {
node.destroy();
}
if (node.parentNode != null) {
MochiKit.DOM.removeElement(node);
}
MochiKit.Signal.disconnectAll(node);
while (node.firstChild != null) {
MochiKit.Widget.destroyWidget(node.firstChild);
}
} else if (MochiKit.Base.isArrayLike(node)) {
for (var i = node.length - 1; i >= 0; i--) {
MochiKit.Widget.destroyWidget(node[i]);
}
}
}
Cheers,
/Per