I am trying to parse text from a dictionary.txt file for a web browser word game. I found How to read a local text file? and used the readTextFile(file)
function the top commentor suggested. However, I don't understand how to get the parsed text into a set.
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)
{
var allText = rawFile.responseText;
alert(allText)
}
}
}
rawFile.send(null);
}
alert(allText)
gives me a popup with words in the dictionary.txt
, so I know the words are getting properly parsed into the variable allText
.
How do I now move these words into a set structure for use in my game? I am thinking I would do this in my main program--run readTextFile
, then move to set.
Also, as this is my first time using JavaScript, when I run readTextFile
in my main program, do I need to do:
myWords = readTextFile("dictionary.txt");
To store allText
intomyWords
, or will simply doing:
readTextFile("dictionary.txt");
Make it so that I can access allText
in my main program? I am unfamiliar with scoping in JS.