I have a txt file containing a list of all Italian words (link) that I want to read and then convert to an array of words, but I'm a bit stuck on the reading part. The file was originally downloaded from the web, so it might or might not have some encoding issues.
To read the file, I am using Fetch, with the same code suggested in the top answer of this post. After reading it, if I use alert(storedText)
the text is correctly displayed; however, if I try var s = storedText.toString()
and then alert(s)
I get "undefined" in the alert box.
I guess there is some problem when reading the file, but I'm rather new to JavaScript and I can't figure out what exactly the problem is. Do you guys have any idea?
Edit: this is my full code
var storedText;
fetch('http://doodlemarty.unaux.com/wp-content/uploads/2021/08/parole.txt')
.then(function(response) {
response.setContentType("text/html;charset=UTF-8");
response.text().then(function(text) {
storedText = text;
done();
});
});
var s = storedText.toString();
var fullList = storedText.split('\n');
function test () {
//first try:
alert(storedText);
//second try:
alert(s);
//trying split:
alert(fullList[2]);
};
I have the test function execute when a button is clicked.