www.webdeveloper.com
Results 1 to 7 of 7

Thread: Javascript getElementsByTagName - IE and Firefox differences

  1. #1
    Join Date
    Jun 2007
    Posts
    4

    Javascript getElementsByTagName - IE and Firefox differences

    Hello,

    I've searched around alot and can't seem to find an answer to this question regarding how to code for Firefox and IE and how they handle the getElementByTagName differently.

    Scenario: I'm creating an RSS reader that shows one <item> at a time, with a next and previous button to view all the other items in the RSS feed (10 total). All done with Javascript / PHP for the backend. The problem comes when I try and work with the XML doc obj. model and Firefox creating text nodes for all the white space. Because I am not looping thru all child nodes I can not test for the node type (or don't know how)

    Given this XML:
    Code:
    <item>
            <title>Gourmet gizmo</title>
            <link>http://www.fresnobee.com/170/story/60024.html</link>
            <guid>http://www.fresnobee.com/170/story/60024.html</guid>
            <pubDate>Fri, 15 Jun 2007 04:53 PDT</pubDate>
            <description>SACRAMENTO -- It can offer a wine at the Davis Food Co-op that goes with roasted turkey, tell you whether hamburger or halibut pairs best with cabernet sauvignon, quote the discount price of a Chilean varietal or tell you all about the vineyard that produced a bottle of Argentinian ros&amp;#233;.</description>
    </item>
    In IE use this javascript to access the title:

    Code:
    var items = xml.getElementsByTagName('item')[0];//grab the first item node
    var title = items.childNodes[0].firstChild.nodeValue;//Gourmet gizmo
    however in firefox, this may or may not be the firstchild (childNodes[0]) and if I wanted the description from the above (items.childNodes[4]) I'm a total loss, because firefox says I have 11 child nodes above and not 5.

    If I haven't already lost you and you know of a simple solution I'm totally willing to entertain any ideas.

    Thanks in advance!

    Greg

  2. #2
    Join Date
    Jun 2007
    Posts
    4
    I'll also mention that I tried using a "gaps cleaner" that found in another post on the board, and it works fine in IE, if I use something like:

    alert(items.childNodes[0].firstChild.nodeValue); //I get the alert in IE but not in Firefox.

    thanks again.

    Greg

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var items = xml.getElementsByTagName('item')[0];//grab the first item node
    var title = items.getElementsByTagName('title')[0].firstChild.data;//Gourmet gizmo

  4. #4
    Join Date
    Jun 2007
    Posts
    4
    Quote Originally Posted by Fang
    Code:
    var items = xml.getElementsByTagName('item')[0];//grab the first item node
    var title = items.getElementsByTagName('title')[0].firstChild.data;//Gourmet gizmo
    Fang, that works like a charm. If I might ask another question, how does

    firstChild.data

    differ from

    firstChild.nodeValue

    Either way, thank you so much for your response!

    Greg

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    data can only return the value of a text node, whereas nodeValue can return the value of other nodes.
    http://developer.mozilla.org/en/docs...eValue#1027970

    In the solution you could have used either.

  6. #6
    Join Date
    Nov 2009
    Posts
    1
    You can try looping through concrete tags.
    var items = data.getElementsByTagName('item');

    for(var i=0; i < items.length; i++) {
    var titles = items[i].getElementsByTagName('title');
    var title = titles[0];
    }

    inline solution:
    var title = data.getElementsByTagName('item')[0].getElementsByTagName('title')[0].nodeValue;

  7. #7
    Join Date
    Dec 2008
    Posts
    488
    Yes, this is a problem everyone runs into with XML at one time or anther. "firstChild", "nextSibling", etc. are very difficult to work with. It might be beneficial to you to get jQuery and jFeed. Running jFeed makes working with RSS about 10 times easier!

    Example-- where you might spend an hour developing 50 lines of code to output a single RSS feed, you could do it in 1 minute and 5-6 lines of code using jFeed:

    http://plugins.jquery.com/project/jFeed

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