# Stop/Timeout xmlhttprequest



## 05026652 (May 5, 2010)

Hey guys, I am working with a bit of code, that will basically read from a text file on a webserver and store each line inside the text file into an array. This array will then be used in some dynamic function I intend to use in the future. For know here is what I got so far


```
<script type = "text/javascript">    
        var txtFile = new XMLHttpRequest();
        txtFile.open("GET", "external.txt", true);
        txtFile.onreadystatechange = function() {
          if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) {  // Makes sure it's found the file.
              allText = txtFile.responseText;
              lines = txtFile.responseText.split("\n"); // Will separate each line into an array
                for (i=0; i<lines.length-1; i++) {
                           //alert("Array index " + i + ": " + myArray[i]);
                document.write("Array index " + i + ": " + lines[i] + " ")
                document.write("<br>")
                }
                
            }
          }
        }
        txtFile.send(null);
        txtFile.setTimeout(10);
    
    </script>
```
Now this works pretty decently - however the page is in continues "load" as if it is waiting for further input or somehting which Im thinking is caused by the open xmlhttprequest.

Is there any way of closing this xmlhttprequest or timing it out after a few seconds.

Appreciated


----------



## MMJ (Oct 15, 2006)

What does the last line do? Did you write this?


```
txtFile.setTimeout(10);
```


----------



## 05026652 (May 5, 2010)

Ah crap, forgot I left that in there.....I wrote that in as a bit of experiment....but it didnt work. Ill remove it now.

Do you have any ideas then?

-------------------------------------

Ok cant edit original post, this is the updated Code:


```
<script type = "text/javascript">    
        var txtFile = new XMLHttpRequest();
        txtFile.open("GET", "external.txt", true);
        txtFile.onreadystatechange = function() {
          if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) {  // Makes sure it's found the file.
              allText = txtFile.responseText;
              lines = txtFile.responseText.split("\n"); // Will separate each line into an array
                for (i=0; i<lines.length-1; i++) {
                           //alert("Array index " + i + ": " + myArray[i]);
                document.write("Array index " + i + ": " + lines[i] + " ")
                document.write("<br>")
                }
                
            }
          }
        }
        txtFile.send(null);    
    </script>
```


----------



## MMJ (Oct 15, 2006)

> Now this works pretty decently - however the page is in continues "load" as if it is waiting for further input or somehting which *Im thinking is caused by the open xmlhttprequest*.


Very doubtful.

Link or full code?


----------



## 05026652 (May 5, 2010)

Yeah, link http://www.empiricink.com/test/home.html

and full code


```
<html>
 <head>
 <script type = "text/javascript"> 
  var txtFile = new XMLHttpRequest();
  txtFile.open("GET", "external.txt", true);
  txtFile.onreadystatechange = function() {
    if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
      if (txtFile.status === 200) {  // Makes sure it's found the file.
        allText = txtFile.responseText;
        lines = txtFile.responseText.split("\n"); // Will separate each line into an arra          
    for (i=0; i<lines.length-1; i++) {
                //document.write("Array index " + i + ": " + lines[i] + " ")
    //document.write("<br>")
    document.write("<a href=" + lines[i] + ">Click Here</a>")
    document.write("<br>")
          }
    
      }
    }
  }
  txtFile.send(null);
  
 </script>
  
 </head>
 <body> 
 
 </body>
<html>
```


----------



## MMJ (Oct 15, 2006)

Don't use document.write

http://www.google.com/#q=document.write+reloads+page


----------



## vipernet (Aug 23, 2007)

Why txt file?, Personally I would suggest using XML file.

something like:


```

```
check the browser, and load the XML using the correct parser
create an HTML table with 


We use getElementsByTagName() to get all XML CD nodes
For each CD node, we display data from ARTIST and TITLE as table data.
We end the table with


----------



## MMJ (Oct 15, 2006)

Why XML?

Unless you have a really good reason to use XML use JSON.

Takes way less space, much easier to work with in almost all languages.


----------

