MozillaZine

node.childNodes returning an undefined node.

Discuss building things with or for the Mozilla Platform.
mesh
 
Posts: 61
Joined: November 28th, 2004, 12:54 pm
January 24th, 2006, 11:55 am

Post Posted January 24th, 2006, 11:55 am

Firefox 1.5
Mac 10.4.4

I am running into an issue where I can't remove a DOM node from another DOM node. Basically, the node is there, but when I access it via its parent's child nodes, it comes back undefined.

Here is the exception I get:

----
Error: uncaught exception: [Exception... "Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER) [nsIDOMHTMLDivElement.removeChild]" nsresult: "0x80004003 (NS_ERROR_INVALID_POINTER)" location: "JS frame :: http://weblogs.macromedia.com/mesh/file ... NAWatch.js :: anonymous :: line 865" data: no]
----


Here is the code snippet

Code: Select all
MXNAWatch.prototype._removeChildrenFromNode = function(node)
{
   if(!this._hasValue(node))
   {
      return;
   }
   
   var len = node.childNodes.length;
   
   for(var i = 0; i < len; i++)
   {   
      node.removeChild(node.childNodes[i]);
   }
}


Basically, it chokes on this line:

node.removeChild(node.childNodes[i]);

because when it hits the node that contains the View All link, node.childNodes[i] returns undefined, and not the node.

You can view the app / widget here:

http://weblogs.macromedia.com/mesh/file ... /ajax_dom/

and the source here:

http://weblogs.macromedia.com/mesh/file ... NAWatch.js

Basically, when you click the little green arrow icon in the top item, i loop through and room all of the child items (basically the post description and view all link).

However, trying to remove the view all link results in the error above.

I had actually ran into this issue when trying to update a Firefox extension to work in Firefox 1.5. I was never able to figure out the issue or a workaround.

Anyone have any idea what might be going on? and how I might be able to work around it?

mike chambers

mesh
 
Posts: 61
Joined: November 28th, 2004, 12:54 pm
January 24th, 2006, 12:24 pm

Post Posted January 24th, 2006, 12:24 pm

fyi, I have reproduced the issue in a much simpler test case:

http://weblogs.macromedia.com/mesh/file ... m/bug.html

Basically, add two nodes, and then click to remove one.

Anyone know what is going on?

mike chambers

Torisugari
 
Posts: 1426
Joined: November 4th, 2002, 8:34 pm
Location: Kyoto, Nippon (GMT +9)
January 24th, 2006, 12:30 pm

Post Posted January 24th, 2006, 12:30 pm

mesh wrote:I was never able to figure out the issue or a workaround.

Then how about
Code: Select all
if (node.childNodes[i])
  node.removeChild(node.childNodes[i]);

?

Basically, fixed (= dead, not alive) length can always be a problem.
Code: Select all
while (node.hasChildNodes()) {
  node.removeChild(node.firstChild);
}

would be another good workaround.

mesh
 
Posts: 61
Joined: November 28th, 2004, 12:54 pm
January 24th, 2006, 12:40 pm

Post Posted January 24th, 2006, 12:40 pm

The second code snippet fixed the issue.

Thanks for the help. So, in general, I should trust length when gettingg the # of child nodes?

mike chambers

mesh
 
Posts: 61
Joined: November 28th, 2004, 12:54 pm
January 24th, 2006, 12:50 pm

Post Posted January 24th, 2006, 12:50 pm

Actually, disregard that question. I just realized what was going on. Basically, as I removed a node, the number of childNodes was reduced, so eventually I was access an index that did not exist.

The odd thing is that prior to 1.5 this was not a problem.

Anyways, thanks for the help...

mike

Torisugari
 
Posts: 1426
Joined: November 4th, 2002, 8:34 pm
Location: Kyoto, Nippon (GMT +9)
January 24th, 2006, 1:10 pm

Post Posted January 24th, 2006, 1:10 pm

mesh wrote:Actually, disregard that question. I just realized what was going on. Basically, as I removed a node, the number of childNodes was reduced, so eventually I was access an index that did not exist.


Yep, I've just remembered we often decrement the index.
Code: Select all
for (var i = len -1; i > -1; i--) {
  node.removeChild(node.ChildNodes[i];
}

should work as well.

Return to Mozilla Development


Who is online

Users browsing this forum: No registered users and 0 guests