Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttpRequest problem

    Hopefully I'm posting this in the correct place. This script is a bit complex but I'm hoping there is a SCRIPT GURU out there who can help me. What I have is a JavaScript that uses a XMLHttpRequest to include info from a series of plain text files identified in an array.

    What the script is supposed to do is go through the array and load the next text file at 3 second [for testing purposes] intervals. It loads the first text file just fine and the timer works. The problem is that it is not advancing through the array like it is supposed to, it just keeps reloading the first text file over and over. Please help - I'm getting a migrane.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Importing from plain text [*.txt] into HTML with XML.</title>
     
    <script language="javascript" type="text/javascript">
    <!--
    //Stop the continual page loading in Firefox, Mozilla, Netscape and possibly older versions of IE.
    function stopLoad(){
    window.stop(); return false;
    document.execCommand('Stop');
    }
    var xmlhttp=false;
     
    //The commented out section below IS REQUIRED AS IS for IE browser engine. DO NOT uncomment it or remove it.
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions and security blocked creation of the objects.
    try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xmlhttp = false;
    }
    }
    @end @*/
     
    //The following section is for cross-browser compatability and allows the script to work in Firefox, Mozilla, Netscape, and Opera browsers.
    function loadData(){
    if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
    try {
    xmlhttp = new XMLHttpRequest();
    stopLoad();
    } catch (e){
    xmlhttp=false;
    window.stop();
    }
    }
    if (!xmlhttp && window.createRequest){
    try{
    xmlhttp = window.createRequest();
    stopLoad();
    }
    catch (e) {
    xmlhttp=false;
    window.stop();
    }
    }
     
    //This section is the actual execution of the script which will write the contents of the *.txt file to the page.
    var txtArray = ["text1","text2","text3","text4","text5","text6"];
    var n = 0;
    if (n == txtArray.length){return}
    fileData = txtArray[n] + ".txt";
    n++
    xmlhttp.open("GET", fileData, true);
    xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4){
    document.getElementById("textDiv1").innerHTML = xmlhttp.responseText;
    setTimeout("loadData()",3000);
    alert(fileData);
    }
    }
    xmlhttp.send(null);
    }
    //-->
    </script>
    </head>
     
    <body onload="loadData();">
    <div name="textDiv1" id="textDiv1"></div>
    </body>
    </html>
    • Jesus called. He says He misses you.

  • #2
    New Coder
    Join Date
    Nov 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    n=0;
    You initialize n everytime you call the function


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •