2013-05-16 84 views
0

我对此有些厌倦。“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)

+0

该错误是哪一行? –

+0

我想,这可能有助于[XMLHTTP-IS-未定义的JavaScript的Ajax] [1] [1]:http://stackoverflow.com/questions/13585807/xmlhttp-is-undefined-javascript- ajax – Dave

回答

3

检查拼写txtfile.send()应该是txtFile.send(),注意f

的资本

更新为第二个错误

错误是因为document.inputtext是未定义的,我假设它的意思是一个html元素输入框。如果这是正确的,则可能值得输入idinputtext,然后用var inputarea = document.getElementById('inputtext')调用它。虽然如果你打算这么做,那么将整个脚本放在window.onload的回调中是值得的,因此脚本运行时输入会出现在页面上。

+0

我是个白痴。谢谢。但是现在我得到一个新的错误。我会更新OP。 – OmniOwl

+0

你认为哪个是最简单的..?我对这一切都很陌生,但是在其他语言方面有经验(C#和Java) – OmniOwl

+0

我给它一个ID,就像你说的那样,然后把整个事物转移到身体中。制作这个回调的东西可能会很聪明,但我真的很新,所以我不知道该怎么做。 – OmniOwl

0

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

+0

你应该在.send(),而不是.open()之前分配它,但是我发现这并不重要 – Ian

+0

我有过相同的经历。我看到人们都说。在开放之前,我总是声明事件处理程序只是为了安全,但像您一样,我从来没有注意到其中的差异。 –

+0

有趣的是,在调用'.send()'之后,jQuery设置'onreadystatechange' **。当我在源代码中看到这些时,我很困惑 – Ian

相关问题