1

I am trying to read in the text from a file and store it as a variable in javascript. Being new to JS, I had no idea this would be so difficult.

First I start an http server with python

python -m http.server

which runs at http://localhost:8000.

Then I am including the following script in my HTML. My plan is to store the text in a hidden <p> element and then access it later in the script. Because the script runs asynchronously, when I get the element's HTML it is still be initial stuff.

<script>

    function readTextFile(file){
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", file, true);
        rawFile.onreadystatechange = function (){
            // Wait until file has loaded (state 4)
            if (rawFile.readyState == 4) {
                var text = rawFile.responseText;
                var e = document.getElementById("myFileText");
                e.innerHTML = text;
                console.log('inside');
            }
        }
        rawFile.send(null);
    };

    readTextFile('http://localhost:8000/static/live-slideshow/image_paths.txt');

    // The following line results in console output of the old internals
    var text = document.getElementById("imagePaths").innerHTML
    console.log(text)

    // Remaining script that uses the "text" variable...


</script>

My questions are:

  1. how can I tell the script to "wait until the readTextFile function has run" before continuing
  2. am I doing this all wrong? (I am only looking to host this thing locally - on my raspberry PI actually)

This is a problem I could solve with Flask or a similar framework but I want to make this as lightweight as possible (preferably no jQuery - but that being said, a nice solution with $'s would still be welcome!)

dvhh
  • 4,724
  • 27
  • 33
Alex
  • 12,078
  • 6
  • 64
  • 74
  • One thing I would change is to use a relative path for the file. That way you can change the hostname, port etc without having to change your code, eg `readTextFile('static/live-slideshow/image_paths.txt')` – Phil Jan 30 '17 at 03:56
  • So you mark this as a duplicate and that is all you say for advice?? I will dig into the link but I think that is a little unfair considering the effort I put forward in asking this – Alex Jan 30 '17 at 04:00
  • @Phil nothing to say? My question is clearly not a duplicate of the one you linked to. They do not address the problem of using `onreadystatechange` as the function trigger – Alex Jan 30 '17 at 04:13
  • 1
    That's the idea behind marking a question as a duplicate. The linked post has many, very high quality answers. I believe you will find what you're looking for there. Whether you use `onreadystatechange` or `onload` does not matter. [This answer](http://stackoverflow.com/a/18309057/283366) addresses that topic directly – Phil Jan 30 '17 at 04:18
  • Please don't think of a *"close as duplicate"* as a bad thing. It's not so much saying your question is exactly like the other question, more so that the answer(s) to your question may be found on an existing post. It keeps StackOverflow concise and helps promote existing, high-quality answers. – Phil Jan 30 '17 at 22:51
  • 1
    I needed some time to come to terms with the fact that I wouldn't be getting a personalized answer. I am trying to learn JS and the answers you linked to are a good resource – Alex Jan 31 '17 at 20:07

0 Answers0