Need Help Fixing XML in My Script

Discussion in 'JavaScript and AJAX' started by iainnitro, May 27, 2010.

  1. iainnitro

    iainnitro New Member

    Joined:
    May 27, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT Consulting
    Location:
    Lawrence, Kansas
    Home Page:
    http://www.teocommunications.com
    Greetings: I have a JavaScript I am working on (and I am not married to the idea of doing this in JS, if there is a better, faster way) and the script does not function in IE (version 8, the only one I had for testing). This script works great in Firefox 3, but it returns "undefined" when executed by IE. I have determined that the text array which is filled from an XML file is empty and therefore the script returns "undefined". I am not sure how to fix the script to work with IE. The Code is below and the XML file code is included as well. Any help on how to fix this script is VERY appreciated.

    Code:
    <html>
    <head>
    
        <script language="JavaScript">
    
        <!--
    var objVals = {
        textArray: null,
        counter: 0,
        randomArray: null,
        xmlURL: "testimonial.xml",
        intervalTime: 2000
    }
    function setText(){
        document.getElementById("testimonials").innerHTML=
            objVals.textArray[objVals.randomArray[objVals.counter]];
        objVals.counter++;
        if(objVals.counter>=objVals.textArray.length){
            objVals.counter=0;
        }
        setTimeout("setText()",objVals.intervalTime);
    }
    
    
    function onLoadFunction(){
        var xmlDoc;
        
        //Code for IE
        if (window.ActiveXObject)
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        
            xmlDoc.async=false;
            xmlDoc.load(objVals.xmlURL);
        }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument)
        {
            xmlDoc = document.implementation.createDocument("","",null);
            
            xmlDoc.async=false;
            xmlDoc.load(objVals.xmlURL);
        }
        
        
    
        var x = xmlDoc.documentElement.getElementsByTagName("text");
        var text = new Array;
        for(i=0;i<x.length;i++){
            text[i]=x[i].textContent;
        }
        // now randomise the array
        // create an array with number 0 to x lenght
        var numbers=new Array();
        for(i=0;i<x.length;i++){
            numbers[i]=i;
        }
        // fill randomNumbers randomly with numbers from numbers array
        //  this will assure that randomNumbers has only unique numbers
        var randomNumbers=new Array();
        i=0;
        while(i<numbers.length){
            var tmp = Math.floor(Math.random() * numbers.length);
            randomNumbers[randomNumbers.length]=numbers[tmp];
            numbers.splice(tmp, 1);
        }
        objVals.textArray=text;
        objVals.randomArray=randomNumbers;
        setText();
    }
    
    
        -->
    
        </script>
    
    </head>
    
    <body onload="onLoadFunction();">
    
    <div id="testimonials">
    </div>
    
    </body>
    </html>
    The XML file that is being parsed:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <testimonials>
        <text>
            This Would be the Text for a Testimonial.
            -- Joe Student, Lawrence, Kansas.
        </text>
        <text>
            Another glowing Testimonial from a person.
            -- Sally Student, Columbus, Ohio.
        </text>
        <text>
            The best site ever!
            -- Johnny Student, Topeka, Kansas
        </text>
    </testimonials>
    The script should just read in all the child tags (text) and then fill the array and rotate them randomly. The script inserts the current array element into the DIV tag.

    Thanks in advance for any help!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice