www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 09-04-2006, 02:29 AM
    michellejg michellejg is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 1
    Question Firefox error with getElementsByTagName()

    Hi

    I've been getting a "has no properties" error within Firefox for the following code. It's loads an XML document, and then pulls out the data from various nodes and further on, plugs them into some generated DOM elements to appear all pretty on the screen. Woo!

    Anyways, here's the code:

    Code:
        function loadXML()
        {
            // If browser is IE, load XMLDOM - else load for Firefox etc
            if (window.ActiveXObject)
            {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async=false;
                xmlDoc.load("humm2.xml");
                getQuestionData(questionNumber, currentNode);
            }
            else if (document.implementation && document.implementation.createDocument)
            {
                xmlDoc= document.implementation.createDocument("","",null);
                xmlDoc.load("humm2.xml");
                xmlDoc.onload=getQuestionData(questionNumber, currentNode);
            }
            else
            {
                alert('Your browser cannot handle this script');
            }
        }
    
        // Retrieve the question data from the XML file
        function getQuestionData(qNumber, currNode)
        {   
            doc = xmlDoc.documentElement;
            alert(doc.length);
            var questionData;
    
            var answerArray = new Array();
    
            if(doc.getElementsByTagName("question")[currNode].attributes[0].value == qNumber)
            {
                questionData = doc.getElementsByTagName("questioncontent")[currNode].firstChild.nodeValue;
                           
                var numOfAnswers = doc.getElementsByTagName("answercontent")[currNode].childNodes.length;
                for(var j = 0; j < numOfAnswers; j++)
                {
                    answerArray[j] = doc.getElementsByTagName("answercontent")[currNode].childNodes[j].childNodes[0].nodeValue;
                }
            }
            
            createPageContent(questionData, answerArray, currNode);
            
            // Increment the question number if it isn't the last question in the XML document
            if(qNumber < doc.childNodes.length)
            {
                questionNumber = qNumber + 1;
                currentNode = currNode + 1;
            }
        }
    The bolded line is where Firefox is falling over. I'm not sure why this is happening. Maybe because it's calling from an XML document, and not the actual document object? (I may screw up terminology here - I'm still a noob in the javascript world )

    I've searched around and the only examples I can find are code samples for document.getElementsByTagName(), nothing to do with XML.

    This script works perfectly in IE, by the way.

    Any clues?

    Cheers,
    Mish
    Reply With Quote
      #2  
    Old 09-04-2006, 03:35 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 12,582
    IE and FF count in different ways the childNodes. FF (DOM compliant mode) counts even the possible textNodes (the gaps between tags) while IE counts only some of them.

    To avoid this, use one the solutions:
    1. use getElementsByTagName() rather than childNodes[]
    2. use a while loop and nodeType attribute to check whether a child is a tag or a text
    3. use a "gaps cleaner" code onload :
    Code:
    var notWhitespace = /\S/;
    function cleanWhitespace(node) {
      for (var x = 0; x < node.childNodes.length; x++) {
        var childNode = node.childNodes[x]
        if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
    // that is, if it's a whitespace text node
          node.removeChild(node.childNodes[x])
          x--
        }
        if (childNode.nodeType == 1) {
    // elements can have text child nodes of their own
          cleanWhitespace(childNode)
        }
      }
    }
    }

    Last edited by Kor; 12-03-2009 at 02:57 AM.
    Reply With Quote
      #3  
    Old 12-02-2009, 04:12 PM
    nicholasyonko nicholasyonko is offline
    Registered User
     
    Join Date: Dec 2009
    Posts: 1
    Thank you

    You function above saved me tons of headache. Thank you.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    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 Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:12 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.