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>