www.webdeveloper.com
Results 1 to 9 of 9

Thread: attributes.length is null in firefox

  1. #1
    Join Date
    May 2010
    Posts
    35

    attributes.length is null in firefox

    Firebug is reporting that my attributes.length is null. Everything is fine in IE8.
    Any Ideas?

    Code:
          var xml = ajaxRequester.responseXML;
          rootNode = xml.documentElement;
          lenNodeAttrs = rootNode.childNodes[0].attributes.length;
          alert(lenNodeAttrs);
    The XML in this instance:

    Code:
    <project id='25J1105A'>
    <site id='9711' class='site-summary' ajaxQueryId='2'>
    <cell></cell><cell class='display-name' colspan='3'>Sharon Hill</cell><cell name='payroll_hours'>1.50</cell><cell name='connect_time'>1.42</cell><cell name='cph'>0.67</cell><cell name='avg_intv_length'>31.80</cell><cell name='r_rate'>16.7%</cell><cell name='c_rate'>16.7%</cell><cell name='efficiency'>90.7%</cell><cell name='calls_made_count'>246</cell><cell name='talk_mins'>51</cell><cell name='talk_percent'>60.3%</cell><cell></cell><cell name='sq_prompt_total'>4</cell><cell name='sq_prompt_percent'>4.2%</cell><cell name='sq_prompt_avg'>2</cell><cell></cell><cell name='break_percent'>0.0%</cell><cell></cell><cell name='meeting_mins'>0</cell><cell name='meeting_percent'>0.0%</cell><cell></cell><cell name='wait_phone_mins'>30</cell><cell name='wait_phone_percent'>35.5%</cell></site>
    </project>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    the DOM attributes property is rather an object than an array, thus it has no length. To circle through the attributes you should rather use a keyed loop, not an indexed one. Try this:

    Code:
    var lenNodeAttrs=0;
    for(a in rootNode.childNodes[0].attributes){
    lenNodeAttrs++;
    }
    alert(lenNodeAttrs)
    Last edited by Kor; 01-05-2011 at 09:35 AM.

  3. #3
    Join Date
    May 2010
    Posts
    35
    Simple as pie for the length value *was* to be used iterating through the attributes in a for loop-dee.

    Thanks

  4. #4
    Join Date
    May 2010
    Posts
    35
    Well, now it works in Firebox but not IE.

    Error in IE: Object doesn't support this action.
    Pointing to line:

    Code:
    for (a in rootNode.childNodes[n].attributes){

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Well, there is an inconsistency regarding the way attributes property is treated by different browsers. See:
    http://www.quirksmode.org/dom/w3c_core.html#attributes

    There's an advice there: "Avoid attributes[]. It is worse than anything else"

    maybe you should try an if/else based on the attributes.length value

    Or you may traverse all the element's properties (native and custom altogether), like
    Code:
    for(a in rootNode.childNodes[0]){
    // now a should return all the properties of the object
    }
    Which is, in fact, your final aim, after all?

  6. #6
    Join Date
    May 2010
    Posts
    35
    Code:
    var lenNodeAttrs=0;
    for(a in rootNode.childNodes[0].attributes){
    lenNodeAttrs++;
    }
    alert(lenNodeAttrs)
    Yeah -- and I thought it was working in firefox. After displaying the attribute count, I realized it doesn't. The returned value is 0 when it should be 3.

    I'm also reading "like arrays, collections have the length property"

    I really need the ability to access the attributes collection. However, getting a count in proving to be a pain with firefox.
    Last edited by objNoob; 01-05-2011 at 10:42 AM.

  7. #7
    Join Date
    May 2010
    Posts
    35
    Kor, the problem was not collections vs arrays. It was the fact that
    xml.documentElement.childNodes[0] is an incorrect reference (in Firefox 3.6.13) to my site node for which I was trying to get the attributes collection.

    There is a .length property of the attributes[] object in both IE 8 and FF 3.6.13

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    well, referencing upon childNodes is always tricky. There are the so called "gaps", the empty spaces between tags. FF counts all of them, as it considers them as possible textNodes, while IE counts only some of them. childNodes[0] might mean a text node, even empty, in FF, but the first tag node in IE. There are several ways to bypass this problem, but the best one is to use getElementsByTagName() to get the tag nodes, not the childNodes.

  9. #9
    Join Date
    May 2010
    Posts
    35
    Yes, I had formatted my XML using white space, and this caused the inconsistent node indexing.

    Removed white space formatting and everything ran smoothly.

    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles