Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Alternative to "children" property of DOM in firefox

Reply
Thread Tools

Alternative to "children" property of DOM in firefox

 
 
Angel
Guest
Posts: n/a
 
      06-19-2006
I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes[i].children.length)

How do I change the second line of code for it to work in firefox?

Regards,
Angel

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      06-19-2006
Angel wrote on 19 jun 2006 in comp.lang.javascript:

> I have the following code
>
> var thisDoc=document.getElementById("myTable"); // myTable is the name
> of the table
> alert(thisDoc.childNodes[i].children.length)
>
> How do I change the second line of code for it to work in firefox?


alert(thisDoc.rows[i].cells.length)

[only partly tested and me not knowing much about FF,
it seems the natural way to DOMinate]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
Robert
Guest
Posts: n/a
 
      06-19-2006
Angel wrote:
> I have the following code
>
> var thisDoc=document.getElementById("myTable"); // myTable is the name
> of the table
> alert(thisDoc.childNodes[i].children.length)
>
> How do I change the second line of code for it to work in firefox?
>
> Regards,
> Angel
>


What about alert(thisDoc.childNodes[i].childNodes.length) ?
And I would expect it to work in IE6 too.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-19-2006
Robert wrote:
> Angel wrote:
>> I have the following code
>>
>> var thisDoc=document.getElementById("myTable"); // myTable is the name
>> of the table
>> alert(thisDoc.childNodes[i].children.length)
>>
>> How do I change the second line of code for it to work in firefox?
>>
>> Regards,
>> Angel
>>

>
> What about alert(thisDoc.childNodes[i].childNodes.length) ?
> And I would expect it to work in IE6 too.


Only if thisDoc.childNodes[i] is not a #text node. It will most likely
be a tableSection element - thead, tfoot or tbody - or maybe a caption,
col or colgroup.


--
Rob
 
Reply With Quote
 
Laurent Bugnion
Guest
Posts: n/a
 
      06-19-2006
Hi,

RobG wrote:
> Robert wrote:
>> Angel wrote:
>>> I have the following code
>>>
>>> var thisDoc=document.getElementById("myTable"); // myTable is the name
>>> of the table
>>> alert(thisDoc.childNodes[i].children.length)
>>>
>>> How do I change the second line of code for it to work in firefox?
>>>
>>> Regards,
>>> Angel
>>>

>>
>> What about alert(thisDoc.childNodes[i].childNodes.length) ?
>> And I would expect it to work in IE6 too.

>
> Only if thisDoc.childNodes[i] is not a #text node. It will most likely
> be a tableSection element - thead, tfoot or tbody - or maybe a caption,
> col or colgroup.


That puzzled me too, but then I thought, the problem will be the same
with children, so hopefully the OP knows what he does.

To handle the issue of the text node, I would introduce some simple
checks like this:

if ( thisDoc
&& thisDoc.childNodes
&& thisDoc.childNodes[ i ]
&& thisDoc.childNodes[ i ].childNodes )
{
alert( thisDoc.childNodes[ i ].childNodes.length );
}

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-19-2006
Laurent Bugnion wrote:
> Hi,
>
> RobG wrote:
>> Robert wrote:
>>> Angel wrote:
>>>> I have the following code
>>>>
>>>> var thisDoc=document.getElementById("myTable"); // myTable is the name
>>>> of the table
>>>> alert(thisDoc.childNodes[i].children.length)
>>>>
>>>> How do I change the second line of code for it to work in firefox?
>>>>
>>>> Regards,
>>>> Angel
>>>>
>>>
>>> What about alert(thisDoc.childNodes[i].childNodes.length) ?
>>> And I would expect it to work in IE6 too.

>>
>> Only if thisDoc.childNodes[i] is not a #text node. It will most
>> likely be a tableSection element - thead, tfoot or tbody - or maybe a
>> caption, col or colgroup.

>
> That puzzled me too, but then I thought, the problem will be the same
> with children, so hopefully the OP knows what he does.
>
> To handle the issue of the text node, I would introduce some simple
> checks like this:
>
> if ( thisDoc
> && thisDoc.childNodes
> && thisDoc.childNodes[ i ]
> && thisDoc.childNodes[ i ].childNodes )
> {
> alert( thisDoc.childNodes[ i ].childNodes.length );
> }


That seems rather a lot of work when it is likely that the OP is after
the table's rows collection, in which case Evertjan's reply fits the
bill: thisDoc.rows.length.

The above sequential test, if it was necessary, could be more efficient as:

var o;
if ( thisDoc
&& (o = thisDoc.childNodes)
&& (o = o[i])
&& (o = o.childNodes) )
{
alert(o.length);
}


--
Rob
 
Reply With Quote
 
William
Guest
Posts: n/a
 
      06-19-2006
I'm not sure this is what you need, but I use in my code something like:

thisDoc.childNodes[i].childNodes.length

and it works pretty well with Firefox, but I would verify if it has
children first with childNodes.hasChildren I think.

------
William


Angel wrote:
> I have the following code
>
> var thisDoc=document.getElementById("myTable"); // myTable is the name
> of the table
> alert(thisDoc.childNodes[i].children.length)
>
> How do I change the second line of code for it to work in firefox?
>
> Regards,
> Angel
>

 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      06-19-2006
RobG wrote:
> Laurent Bugnion wrote:
>
>> Hi,
>>
>> RobG wrote:
>>
>>> Robert wrote:
>>>
>>>> Angel wrote:
>>>>
>>>>> var thisDoc=document.getElementById("myTable"); // myTable is the name
>>>>> of the table
>>>>> alert(thisDoc.childNodes[i].children.length)
>>>>>
>>>>> How do I change the second line of code for it to work in firefox?
>>>>
>>>> What about alert(thisDoc.childNodes[i].childNodes.length) ?
>>>> And I would expect it to work in IE6 too.
>>>

>> To handle the issue of the text node, I would introduce some simple
>> checks like this:
>>
>> if ( thisDoc
>> && thisDoc.childNodes
>> && thisDoc.childNodes[ i ]
>> && thisDoc.childNodes[ i ].childNodes )
>> {
>> alert( thisDoc.childNodes[ i ].childNodes.length );
>> }

>
>
> That seems rather a lot of work when it is likely that the OP is after
> the table's rows collection, in which case Evertjan's reply fits the
> bill: thisDoc.rows.length.
>


I have to give a warning. IE6 can give a "Pure Virtual Call" error and
crash when mixing Core DOM methods and HTML Table DOM methods.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-20-2006
Robert wrote:
> RobG wrote:
> >>>> Angel wrote:
> >>>>
> >>>>> var thisDoc=document.getElementById("myTable"); // myTable is the name

[...]
> > That seems rather a lot of work when it is likely that the OP is after
> > the table's rows collection, in which case Evertjan's reply fits the
> > bill: thisDoc.rows.length.
> >

>
> I have to give a warning. IE6 can give a "Pure Virtual Call" error and
> crash when mixing Core DOM methods and HTML Table DOM methods.


I'd expect that if that's true then it would have been mentioned
here before, do you have an example? As far as I can determine, it
hasn't. Microsoft themselves recommend using the DOM HTML Table
interface for manipulating tables:

<URL; http://msdn.microsoft.com/workshop/author/tables/buildtables.asp>


Note: their documentation for insertRow has so many errors I wouldn't
be surprised if they've got it completely backwards:

<URL:
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertrow.asp>

--
Rob

 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      06-20-2006
RobG wrote:
> Robert wrote:
>
>>I have to give a warning. IE6 can give a "Pure Virtual Call" error and
>>crash when mixing Core DOM methods and HTML Table DOM methods.

>
>
> I'd expect that if that's true then it would have been mentioned
> here before, do you have an example? As far as I can determine, it
> hasn't. Microsoft themselves recommend using the DOM HTML Table
> interface for manipulating tables:
>
> <URL; http://msdn.microsoft.com/workshop/author/tables/buildtables.asp>
>


I was confronted by this problem and saw IE6 crash consistently (tried
many computers) for no apparent reason. After debugging I traced the
problem to a DOM call. There was no reason for it to crash. I googled
and someone mentioned that this could happen when mixing those different
DOM methods. I had nothing to loose so I replaced the HTML Table DOM
Methods with Core DOM methods. I remember especially that I changed
using the rows and cells collection, so then doing it the hard way by
stepping through the childNodes. After this change the problems were gone.

If I find this google reference or the code I changed in CVS maybe I can
tell more.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quicktime Alternative, RealPlayer Alternative & Media Player Classic John Capleton Computer Support 3 12-05-2005 07:41 AM
Convert a XML DOM Object to a HTML DOM Object manjunath.d@gmail.com XML 0 09-20-2005 08:16 AM
What is the difference between DOM Level 1 and DOM Level 2. mike XML 1 11-20-2004 03:19 PM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger XML 0 07-28-2004 08:51 AM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger Java 0 07-28-2004 08:51 AM



Advertisments