-1

I've got some code that's reading an XML document and then using that to build the HTML page. Similar to markdown I suppose. I've simplified the below code but effectively that JS line at the end with CAROUSEL in it is looking at the XML, but it is creating 7 carousel divs instead of 1 like I want. I get why it's returning 7 times (sort of), but how do I get it to only create it once. the ITEM tags inside of the CAROUSEL tag (see the XML section) is to indicate what images should be inside that particular carousel.

JS:

         var col9div = null;
    if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            var  xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET",'xml/index'+page_counter+".xml",false);
        xmlhttp.send();
        xmlDoc=xmlhttp.responseXML;
        var col9div = document.createElement("div");

                                });
                                var tempvar = arr.length;
                                console.log(tempvar);
        $(col9div).addClass("col-md-9");
        $("#bannersize").append(col9div);
        flush();
        function flush(){
            var activity_element_idcounter = 0;
            var module_element_idcounter = 0;
            var x=xmlDoc.getElementsByTagName("MODULE");

            for (i=0;i<x.length;i++)
            {
                var getlastli = $(".sidecounter:last");

                module_element_idcounter++;
                col9div.insertAdjacentHTML('beforeend', '<div class="row"><div class="col-md-12 well"' + ' id="module' + module_element_idcounter + '"><div id="skrollr-div' + module_element_idcounter + '"></div></div>');
                var scanner = x[i].getElementsByTagName("*");
                for (var q=0;q<scanner.length;q++){
                    activity_element_idcounter ++;

                $.each(scanner[q].childNodes, function(){
else if (scanner[q].nodeName === "CAROUSEL"){
do something here
}

XML:

<MODULE>
        <CAROUSEL>
                <ITEM>assets/images/index5/tehran-carousel/tehran-day-and-night.jpg</ITEM>
                <ITEM>assets/images/index5/tehran-carousel/tehran-day-and-night-1.jpg</ITEM>
                <ITEM>assets/images/index5/tehran-carousel/tehran-bazaar-entrance.jpg</ITEM>
        </CAROUSEL>
</MODULE>

thanks, Robbie

Lumnock
  • 1
  • 1
  • 2
    Is there something missing from the JS you posted, like an `if` statement or `for` loop? What you posted doesn't seem to make any sense. And why aren't you using `getElementsByTagName("CAROUSEL")`? – JLRishe Feb 21 '14 at 03:02

1 Answers1

0

I assume this is executing in some kind of loop that you haven't shown us, but you are not using any conditional logic. What you seem to be getting is three separate statements:

// Always just evaluates to false and does nothing; the return value of
// getElementsByTagName() does not have a nodeName property
xmlDoc.getElementsByTagName("MODULE").getElementsByTagName("*").nodeName === "CAROUSEL"

{
    // Always executes - simply a statement inside some curly braces
    $("#module" + module_element_idcounter).append("...");
}

// Empty statement - does nothing
;

To get it to work the way you want it to, you probably need to use an if statement somewhere, but in order for us to help you, you need to show us more of your code than the tiny sampling you have provided.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • You're quite right, and apologies for not including it initially. There is indeed an else if statement for each different tag type. This means that it iterates through the XML document quite a few times but at least it was keeping the HTML document in the same order as the XML document. – Lumnock Feb 21 '14 at 03:15
  • You still haven't shown us remotely enough of your code for us to have any idea how to fix it. Adding an `else if` (without even including the full `if` statement. Does not help much. – JLRishe Feb 21 '14 at 03:16
  • The rest of the code is nearly the same as the above logic, but more of it. xmlDOC is just the XML document that is structured in exactly the same way throughout as the above, with block, and then some tags inside each MODULE such as , – Lumnock Feb 21 '14 at 03:20
  • I just added a lot more of my code, so I hope that helps explain a bit more. – Lumnock Feb 21 '14 at 03:28