2011-04-12 82 views
0

鉴于所有文件(html文件,文本文件等)都在网络上, 有没有什么方法可以读取文本文件并使用ActiveX旁边的textarea打印它们?除了使用ActiveX之外,有没有什么方法可以在javascript中读取文本文件?

我已经试过这样的,但它没有达到的目标:

 
function getSelectedItem(){ 
    var client = new XMLHttpRequest(); 

    if(document.codeForm.dropList.value == "foo") 
     client.open('GET', 'foo.txt'); 
    else if(document.codeForm.dropList.value == "bar") 
     client.open('GET', 'bar.txt'); 
    client.onreadystatechange = function() { 
     //This actually displays the message in the file 
     alert(client.responseText); 

     //But this doesn't. This just displays "undefined" 
// document.codeForm.source.value = client.reponseText; 
    } 
    client.send(); 
} 

因为我实际上可以显示与文件上下文警报消息,我相信会有一些方法来做到这一点。 (实际上文件的内容似乎进入“client.reponseText”, ,但它的数据类型是DOMstring,不只是字符串。)

任何意见将不胜感激。 谢谢。

回答

1

使用jQuery。 http://api.jquery.com/jQuery.get/

$.get("http://www.whatever.com/foo.txt", null, function(response){ 
    $("#theTextArea").val(response); // where theTextArea is the ID of the textarea you want to put the data into. 
}); 
+0

这完美的作品!谢谢! – devEvan 2011-04-13 04:33:06

+0

@Mike Ruhlin-wat如果txt和html文件都存在于系统本地 – Varun 2011-09-29 06:51:28

0

试试这个,而不是

document.codeForm.source.innerValue = client.reponseText; 

document.getElementById("source").innerHtml = client.responseText; 

document.getElementById("source").innerText = client.responseText; 

您的textarea需要一个id属性,使用后两种方法

+0

我刚刚尝试过,但它并没有给textarea带来任何改变。 – devEvan 2011-04-12 12:28:24

+0

我刚更新它添加其他方式来设置文本区 – 2011-04-12 20:58:59

+0

他们仍然领导“未定义”。无论如何,我使用jQuery修复了这个问题。谢谢。 – devEvan 2011-04-13 04:34:40

相关问题