0

I am trying to read a local file called start.txt

If I reach a point where the first line is nextpage, it'll do something but if isn't it will append to a textarea. However it gives me an error:

Uncaught TypeError: Cannot read property 'inputtext' of undefined (23:44:44:520 | error, javascript) at (public_html/index.html:12:50)

The file that I am trying to read is in the same folder as the index.html file. I am fairly new to HTML, AJAX and JavaScript so I might possibly just not know what the frak I am doing!

<!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.textareaform.inputtext.value;
            txtFile.open("GET", "http://localhost/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 += s;
                            }
                        }
                    }
                }
            };
            txtfile.send(null);
        </script>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>
        <form name="textareaform">
            <textarea name="fileoutput" rows="4" cols="20" readonly="readonly">

            </textarea>
        </form>

        <div>TODO write content</div>
    </body>
</html>
4

2 回答 2

1

它正在寻找一个textarea名为“inputtext”,而您将其命名为“fileoutput”。

尝试将您的 textarea 重命名为“inputtext”:

<textarea name="inputtext" rows="4" cols="20" readonly="readonly"></textarea>

或者

以不同的方式定义变量:

var inputarea = document.textareaform.fileoutput.value;
于 2013-05-16T21:56:12.573 回答
0

将 var inputarea 声明更改为此。

var inputArea = document.getElementsByName('fileoutput');

希望这可以帮助。

于 2013-05-16T21:59:01.637 回答