After attempting to extract a list of words from a txt file and store it in a JavaScript variable for future use, I've encountered an issue. The problem lies in my inability to transfer the variable outside of the onreadystatechange function. Could there be a simple solution that has eluded me?
Original Source:
var xmlhttp;
var list = new Array();
var word;
if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
list = xmlhttp.responseText.split("\n");
document.getElementById("testfield").innerHTML = list[0]; //This works
word = list[0];
}
}
xmlhttp.open("GET","wordlist.txt",true);
xmlhttp.send();
document.getElementById("testfield").innerHTML = word; //This doesn't work