未捕获的RefereceError: txtfile未定义
"Uncaught RefereceError: txtfile is not defined"
我有点烦了。
我不知道为什么它一直告诉我文件没有定义,因为它的声明和使用都在同一个作用域中。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<script type="text/javascript">
var txtFile = new XMLHttpRequest();
var inputarea = document.inputtext;
txtFile.open("GET", "start.txt", true);
txtFile.onreadystatechange = function() {
// Makes sure the document is ready to parse.
if(txtFile.readyState === 4) {
// Makes sure it's found the file.
if(txtFile.status === 200) {
allText = txtFile.responseText;
// Will separate each line into an array
lines = txtFile.responseText.split("'n");
for(i = 0; i < lines.length; i++) {
var s = lines[i];
if(s.indexOf("nextpage") > -1) {
// Line is there
} else {
// Line is not there
inputarea.value += s;
}
}
}
}
}
txtfile.send();
</script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<textarea name="inputtext" rows="4" cols="20" readonly="readonly">
</textarea>
<div>TODO write content</div>
</body>
</html>
编辑
问题已经解决,虽然我现在得到另一个错误:
Uncaught TypeError: Cannot read property 'value' of undefined (00:27:29:739 | error, javascript)
at txtFile.onreadystatechange (public_html/index.html:29:42)
检查你的拼写txtfile.send()
应该是txtFile.send()
,注意f
的大写
更新第二个错误
错误是因为document.inputtext
是未定义的,我认为它的意思是一个html元素输入框。如果这是正确的,那么可能值得给输入一个inputtext
的id
,然后用var inputarea = document.getElementById('inputtext')
调用它。虽然如果要这样做,将整个脚本放在window.onload
回调中是值得的,这样当脚本运行时,输入就会出现在页面上。
您应该在调用txtFile.open
之前定义txtFile.onreadystatechange
。不知道这是不是你的问题
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- Jquery未定义函数正在停止其他操作
- AngularJS指令出错-无法读取属性'编译'的未定义
- 这.SOMETHING 总是返回未定义的 - extjs
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- Wordpress中带有JQuery Accordion的未定义匿名函数
- 未捕获的RefereceError: txtfile未定义