Alternative to "children" property of DOM in firefox

Discussion in 'Javascript' started by Angel, Jun 19, 2006.

  1. Angel

    Angel Guest

    I have the following code

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

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

    Regards,
    Angel
     
    Angel, Jun 19, 2006
    #1
    1. Advertising

  2. Angel

    Evertjan. Guest

    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.children.length)
    >
    > How do I change the second line of code for it to work in firefox?


    alert(thisDoc.rows.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)
     
    Evertjan., Jun 19, 2006
    #2
    1. Advertising

  3. Angel

    Robert Guest

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


    What about alert(thisDoc.childNodes.childNodes.length) ?
    And I would expect it to work in IE6 too.
     
    Robert, Jun 19, 2006
    #3
  4. Angel

    RobG Guest

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

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


    Only if thisDoc.childNodes 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
     
    RobG, Jun 19, 2006
    #4
  5. 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.children.length)
    >>>
    >>> How do I change the second line of code for it to work in firefox?
    >>>
    >>> Regards,
    >>> Angel
    >>>

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

    >
    > Only if thisDoc.childNodes 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
     
    Laurent Bugnion, Jun 19, 2006
    #5
  6. Angel

    RobG Guest

    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.children.length)
    >>>>
    >>>> How do I change the second line of code for it to work in firefox?
    >>>>
    >>>> Regards,
    >>>> Angel
    >>>>
    >>>
    >>> What about alert(thisDoc.childNodes.childNodes.length) ?
    >>> And I would expect it to work in IE6 too.

    >>
    >> Only if thisDoc.childNodes 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)
    && (o = o.childNodes) )
    {
    alert(o.length);
    }
    :)

    --
    Rob
     
    RobG, Jun 19, 2006
    #6
  7. Angel

    William Guest

    I'm not sure this is what you need, but I use in my code something like:

    thisDoc.childNodes.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.children.length)
    >
    > How do I change the second line of code for it to work in firefox?
    >
    > Regards,
    > Angel
    >
     
    William, Jun 19, 2006
    #7
  8. Angel

    Robert Guest

    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.children.length)
    >>>>>
    >>>>> How do I change the second line of code for it to work in firefox?
    >>>>
    >>>> What about alert(thisDoc.childNodes.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.
     
    Robert, Jun 19, 2006
    #8
  9. Angel

    RobG Guest

    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
     
    RobG, Jun 20, 2006
    #9
  10. Angel

    Robert Guest

    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.
     
    Robert, Jun 20, 2006
    #10
  11. Angel

    Robert Guest

    Robert wrote:
    > 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>
    >>

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


    I think this is the thread that I found which made me change my javascript.
    http://groups.google.com/group/micr..._frm/thread/d944e6e9b7aa11f4/988675fc965b0500

    "I was mixing DOM calls with table object model enumerations. Using only
    DOM fixed it."
     
    Robert, Jun 20, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Anderson
    Replies:
    2
    Views:
    293
  2. DaRik
    Replies:
    1
    Views:
    81
    Jim Ley
    Jun 1, 2004
  3. DaRik

    DOM children & events

    DaRik, Jun 10, 2004, in forum: Javascript
    Replies:
    2
    Views:
    105
  4. Todd Cary

    Is "children" a valid property in Firefox?

    Todd Cary, Feb 3, 2005, in forum: Javascript
    Replies:
    1
    Views:
    93
    Martin Honnen
    Feb 3, 2005
  5. Thierry Lam
    Replies:
    7
    Views:
    154
Loading...

Share This Page