Before posting, I've read through suggested similar questions, but couldn't find an answer.
I have an array declaration stored in a text.txt file. Let's say the array declaration looks something like this:
["content1-1", ["content2-1", "content2-1"]]
I want to load this as an array into a variable in JavaScript.
This is what I tried:
var data = []
function loadSOAFile(chineseCharacter) {
var textfileFileName = "text.txt"
var rawSOAFile=new XMLHttpRequest();
rawSOAFile.open("GET",textfileFileName);
rawSOAFile.onload=function(){
console.log(rawSOAFile.responseText);
data = rawSOAFile.responseText
}
rawSOAFile.send();
setTimeout(function() {
document.getElementById("debuggingconsole").innerHTML = data[0]
}, 200);
}
However, when I call the function, instead of writing "content1-1" it writes "[", so clearly it loads the variable as a string rather than an array.
Is there a cleaver way to load it as an array declaration? I don't deal with JS all that often, so answers providing a little bit more context would be most appreciated.
setTimeout()to accessdata[0], perform the assignment toinnerHTMLwithin theonloadcallback so that you're not introducing a race-conditon in your code.