node.childNodes returning an undefined node.

Discuss building things with or for the Mozilla Platform.
Locked
mesh
Posts: 61
Joined: November 28th, 2004, 12:54 pm

node.childNodes returning an undefined node.

Post by mesh »

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

Post by mesh »

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: 1634
Joined: November 4th, 2002, 8:34 pm
Location: Kyoto, Nippon (GMT +9)
Contact:

Re: node.childNodes returning an undefined node.

Post by Torisugari »

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

Post by mesh »

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

Post by mesh »

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: 1634
Joined: November 4th, 2002, 8:34 pm
Location: Kyoto, Nippon (GMT +9)
Contact:

Post by Torisugari »

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.
ethanlifka
Posts: 3
Joined: May 5th, 2007, 11:13 pm

Re: node.childNodes returning an undefined node.

Post by ethanlifka »

I have noticed that FF has a bug.

when accessing childNodes it will return any spaces or new lines between real nodes and a undefine node.


Example:

Code: Select all

<div id="parent>
     <div id="child1"></div>
     <div id="child2"></div>
</div>


this will resault in ID Parent having 4 nodes

Solution: ( remove all spaces between the start and end tags.)

Code: Select all

<div id="parent><div id="child1"></div><div id="child2"></div></div>


This may not look nice but it works.

FF please fix this bug. This has been the case from version 3.6 - 4.0.1
User avatar
DanRaisch
Moderator
Posts: 127506
Joined: September 23rd, 2004, 8:57 pm
Location: Somewhere on the right coast

Re: node.childNodes returning an undefined node.

Post by DanRaisch »

1. This forum is not run by or formally associated with Mozilla. We're an independent user community. We didn't write the application and we're not the ones who would fix anything.

2. Thanks for your report but you've replied to a thread that's been dormant for more than 5 years! Locking this due to the age of the original posts.
Somewhere on the right coast.
Locked