1
我想从txt文件中读取单词列表,将其用于稍后在脚本中使用的Javascript变量。但是,我无法将变量传递给onreadystatechange函数。是否有一些简单的步骤,我错过了?AJAX/Javascript - 将txt文件的内容传递给Javascript变量
来源:
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