Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/forums/javascript/)
-   -   Need Help Fixing XML in My Script (http://www.go4expert.com/forums/help-fixing-xml-script-t22220/)

iainnitro 28May2010 01:01

Need Help Fixing XML in My Script
 
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!


All times are GMT +5.5. The time now is 23:58.