Skip to content Skip to sidebar Skip to footer

Read Local Text Title Line-by-line, Store Lines In An Array

I have a local text file wordsEn.txt that is all words in the dictionary (lowercased): a aah aahed aahing aahs aardvark [etcetera] On page load, I would like those to be placed i

Solution 1:

Ajax is Asynchronous. you can't return the responseText from get_words. You should instead use callbacks.

function get_words(callback) {
    var rawFile = new XMLHttpRequest();
    rawFile.open("POST", "wordsEn.txt", true);
    rawFile.onreadystatechange = function () {
        if (rawFile.readyState === 4) {
            if (rawFile.status === 200 || rawFile.status == 0) {
                callback(rawFile.responseText);
            }
        }

    }
    rawFile.send(null);
}

get_words(function (resp) {
    document.getElementById("spresdiv").innerHTML += "<p>" + resp + "</p>";
});

Post a Comment for "Read Local Text Title Line-by-line, Store Lines In An Array"