removing nodes innerHTML vs. removeChild

T

Tim

Hey, so I have heard that it is better to use innerHTML to remove
nodes as opposed to removeChild but only in IE because of a problem
with circular references. Code I found in Ext supports this:

removeNode : isIE ? function(){
var d;
return function(n){
if(n && n.tagName != 'BODY'){
d = d || document.createElement('div');
d.appendChild(n);
d.innerHTML = '';
}
}
}() : function(n){
if(n && n.parentNode && n.tagName != 'BODY'){
n.parentNode.removeChild(n);
}
},

I also found some code which I currently use (I think from Douglas
Crockford) which is suppose to break the circular references:

function purge(d) {
var a = d.attributes, i, l, n;
if (a) {
l = a.length;
for (i = 0; i < l; i += 1) {
n = a.name;
if (typeof d[n] === 'function') {
d[n] = null;
}
}
}
a = d.childNodes;
if (a) {
l = a.length;
for (i = 0; i < l; i += 1) {
purge(d.childNodes);
}
}
}

Now I know the first one uses browser sniffing so thats no good, but
my real question is what solves the problem best?

Any suggestions?
Thanks
 
S

SAM

Tim a écrit :
Now I know the first one uses browser sniffing so thats no good, but
my real question is what solves the problem best?

To do not have circular references ?

The 2nd way seems to destroy all what could be a function hidden in the
tag (in case it could be a closure causing memory leak in IE *and*
Windows ... when will they make something about this trouble ?)

I do not understand, to quit the page stops the circular reference, why
removing the element doesn't do so ?

element -> event -> jscript -> element

if one of the protagonists is removed the circle is broken, no ?
 
R

RobG

Hey, so I have heard that it is better to use innerHTML to remove
nodes as opposed to removeChild but only in IE because of a problem
with circular references.

As far as I know, neither innerHTML or removeChild will break circular
references in IE. Removing the node involved in a circular reference
before breaking it means that you can't break it later and ensures the
IE memory leak issue (which is the real problem) is not fixed.
Code I found in Ext supports this:

Does it? You should ask in an Ext forum. There might be other reason,
none are stated in the code posted (e.g. it may be that since
innerHTML is an IE proprietary property and the authors could not
determine a suitable feature test, they decided to use browser
sniffing - they then assume support in IE and guess that maybe it
isn't supported in non-IE browsers and use DOM methods there).

[...]
I also found some code which I currently use (I think from Douglas
Crockford) which is suppose to break the circular references:
[...]


Now I know the first one uses browser sniffing so thats no good, but
my real question is what solves the problem best?

Since there is no evidence that using innerHTML removes circular
references and hence fixes the associated IE memory leak, I'd keep
using the purge function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top