2

I am following an old stackoverflow post on how to read in a text file using javascript seen here: Javascript - read local text file and I cannot get it to work. I am very new to javascript and all I want to do is store the file to a var and display it on screen so I know it is being read in and split correctly. What am I doing wrong? I know there are many new ways to do this but this is a very early attempt at javascript for me and I found this code to be the most readable example I found online. Im not sure if it matters but the file im trying to read from is about 300 pages of gps data.

  • I tried:
  • pasting the file path into my browser (works fine)
  • removing the state and status checks (no difference.... and bad practice I know)
  • changing browsers (no difference, though I have been using Chrome)
    var allText;
    
    function readTextFile(file)
    {
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    rawFile.send(null);
    }
    readTextFile("file:////var/www/html/gpsPoints.txt");
    locations=allText.split(",");
    
  • Community
    • 1
    • 1
    Rilcon42
    • 9,584
    • 18
    • 83
    • 167
    • This shouldn't work in chrome (without config changes), but some other browsers may allow it. – Kevin B Jun 04 '14 at 16:07
    • 1
      And then, you aren't properly receiving the response text. `locations` is empty and will always be empty with the code you are using. – Kevin B Jun 04 '14 at 16:08

    0 Answers0