Read a data file from the serverTag(s): Language


for Netcape 4
<HTML><HEAD>
<SCRIPT language = "Javascript">
var data = new Array();
var i = 0;
var datafile = window.location.href.substring(0,
     window.location.href.lastIndexOf("/") + 1) +
     "data.txt";
var url = new java.net.URL(datafile);
var connect = url.openConnection();
var input = new java.io.BufferedReader(
     new java.io.InputStreamReader(
         connect.getInputStream()));
var aLine = ""
while((aLine = input.readLine()) != null) {
    data[i++] = aLine;
    }
</SCRIPT>
<BODY>

<SCRIPT language = "Javascript">
var temp = ""
for (var j = 0; j < data.length ; j++)
    temp += data[j] + "<br>";
document.write(temp);
</SCRIPT>
</BODY></HTML>
The data file for demonstration
[data.txt]
one
two
three
four
five
Try it here.

For Internet Explorer

<HTML><HEAD>
<SCRIPT language = "Javascript">

objXml = new ActiveXObject("Microsoft.XMLHTTP");

// objXml = new ActiveXObject("Msxml2.XMLHTTP"); older version
var datafile = "data.txt";

objXml.open("GET", datafile, true);
objXml.onreadystatechange=function() {
   if (objXml.readyState==4) {
       display(objXml.responseText);
  }
 }

objXml.send(null);

function display(msg) {
  alert(msg);
}

</SCRIPT>
<BODY></BODY>
</HTML>
Try it here.

For Netscape 6

<HTML><HEAD>
<SCRIPT language = "Javascript">

if (document.getElementById){
     objXml = new XMLHttpRequest();
     objXml.open("GET",datafile,false);
     objXml.send(null);
     alert(objXml.responseText;
     }
</SCRIPT>
<BODY></BODY>
</HTML>

See also this related HowTo


blog comments powered by Disqus