I know you asked for Ajax/JavaScript but Java Applets work with most desktop browsers and this task would be quite simple in Java, so I've made you an example of how you would do that with a Java applet.
// PHP/HTML embed code
<APPLET CODE="readTextFile.class" width=400 height=300>
<PARAM NAME="fileToRead" VALUE="<?php echo $textfile ?>">
Your browser does not support the <code>applet</code> tag.
</APPLET>
You will need to compile the java file like javac "path/to/readTextFile.java"
in cmd.exe
// readTextFile.java
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class readTextFile extends Applet {
String fileToRead = "path/to/myfile.txt";
StringBuffer strBuff;
TextArea txtArea;
public void init(){
txtArea = new TextArea(300, 400);
txtArea.setEditable(false);
add(txtArea, "center");
// First try the HTML applet parameter, if not use fileToRead variable
String prHtml = this.getParameter("fileToRead");
if (prHtml != null) fileToRead = new String(prHtml);
// Set up a timer to read the file every 20 seconds
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
readFile();
}
}, 0, 20*1000);
}
public void readFile(){
String line;
URL url = null;
try{
url = new URL(getCodeBase(), fileToRead);
} catch (MalformedURLException e) {
//handle or do nothing
}
try {
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
strBuff = new StringBuffer();
while((line = bf.readLine()) != null){
strBuff.append(line + "\n");
}
txtArea.append("File Name : " + fileToRead + "\n");
txtArea.append(strBuff.toString());
} catch(IOException e) {
e.printStackTrace();
}
}
}
That will read the file every 20 seconds from your server. Just make sure the file you try to access is in the same folder or below (but not above) wherever you place readTextFile.class
Note that the text file will get just as many crazy hits (but theres no way around that) but your page wont get crazy hits.