未捕获的RefereceError: txtfile未定义

"Uncaught RefereceError: txtfile is not defined"

本文关键字:txtfile 未定义 RefereceError      更新时间:2023-09-26

我有点烦了。

我不知道为什么它一直告诉我文件没有定义,因为它的声明和使用都在同一个作用域中。

<!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元素输入框。如果这是正确的,那么可能值得给输入一个inputtextid,然后用var inputarea = document.getElementById('inputtext')调用它。虽然如果要这样做,将整个脚本放在window.onload回调中是值得的,这样当脚本运行时,输入就会出现在页面上。

您应该在调用txtFile.open之前定义txtFile.onreadystatechange。不知道这是不是你的问题