faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

42 of 55 people (76%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I display text files in a textarea?

Apr 4th, 2000 06:40
Martin Honnen, Markus Finiefs,


The following builds on the techniques described in
http://www.faqts.com/knowledge-base/view.phtml/aid/1268/fid/126
to load text files with NN4 and IE5; the downloaded html source is 
simply displayed in a textarea:

<html xmlns:msie>
<msie:download id="downloader" 
               style="behavior:url(#default#download)" />
<HEAD>
<SCRIPT>
function fetchURL(url) {
    if ((location.host == '' && url.indexOf(location.protocol) == -1)   
         ||
         url.indexOf(location.host) == -1) 
      {
      netscape.security.PrivilegeManager.enablePrivilege
("UniversalConnect");
      }
    var dest = new java.net.URL(url);
    var dis = new java.io.DataInputStream(dest.openStream());
    var res = "";
    while ((line = dis.readLine()) != null) {
      res += line;
      res += java.lang.System.getProperty("line.separator");
    }
    dis.close();
    return res;
  }
</SCRIPT>
<SCRIPT>
function loadFile (fileName) {
  if (document.layers) {
    var i = new Image();
    i.src = fileName;
    var fileURL = i.src;
    document.formName.file.value = fetchURL(fileURL);
  }
  else if (document.all && document.getElementById)
    downloader.startDownload(fileName, displayFile);
}
function displayFile (text) {
  document.formName.file.value = text;
} 
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<SELECT NAME="files"
        ONCHANGE="if (this.selectedIndex > 0) 
                    loadFile(this.options[this.selectedIndex].value);"
>
<OPTION>-- Select a file to display --
<OPTION VALUE="file1.html">whatever
<OPTION VALUE="file2.html">whatelse
</SELECT>
<BR>
<TEXTAREA NAME="file" ROWS="30" COLS="80" WRAP="off"></TEXTAREA>
</FORM>
<IFRAME NAME="anIframe" SRC="about:blank"></IFRAME>
</BODY>
</HTML>