开发者

Is there any way to read a text file in javascript beside using ActiveX?

Given all the files (html file, text files, etc) are on the web, is there any way to read text file and print them in a textarea beside using ActiveX?

I've tried like this, but it didn't reach the goal:

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 th开发者_C百科is doesn't. This just displays "undefined"
//    document.codeForm.source.value = client.reponseText;
   }
   client.send();
}

Since I could actually display alert message with the file context, I believe there would be some way to do this. (Actually the contents of files seem to come into "client.reponseText", but it's data type is DOMstring, not just String.)

Any advice would be very appreciated. Thanks.


Use 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.
});


try this instead

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

or

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

or

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

your textarea will need an id attribute to use the last two methods

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜