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 into an array:
words = ["a", "aah", "aahed", "aaching", "aahs", "aardvark", ...];
Here's the function I made to return the response code when trying to read the text file:
function get_words()
{
var rawFile = new XMLHttpRequest();
rawFile.open("POST", "wordsEn.txt", true);
rawFile.onreadystatechange = function ()
{
if (rawFile.readyState === 4)
{
if (rawFile.status === 200 || rawFile.status == 0)
{
return rawFile.responseText;
}
}
}
rawFile.send(null);
}
I was trying to copy the procedure here: Javascript - read local text file
(I confess that I copied it without really understanding what it does.)
Apparently it's not working, because
var resp = get_words();
document.getElementById("spresdiv").innerHTML += "<p>" + resp + "</p>";
writes undefined
inside the spresdiv
div. Any idea why this is and how I can fix it?
On a related note, does anyone know if JavaScript arrays are implemented with a tree or any other type of fast-lookup? Does indexOf(...)
use a linear search?