1

I need to know, is it possible to find a file in web server is updated using javascript. In my java application i am updating one file using java if any error occurs say, like logger.. By using javascript i need to read that file immediately if any updates occured and show it in a web page... First i need to know is it possible to do that ??? If so, Can u come up with a idea to do that??? If possible with a clear code or example.... Thanx in advance..

2 Answers2

3

So long as the file is a static file and hosted in the web root, you could use a HEAD request to inspect the date and time of file.

However, because JavaScript uses the date and time of the client you should bear in mind any timezones for the client.

At this point you'd have to poll the server every n seconds to check for updates.

HEAD request is already dealt with here: HTTP HEAD Request in Javascript/Ajax?

A HEAD request returns something like this:

HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Cache-Control: max-age=172800
Expires: Sat, 06 Apr 2002 11:34:01 GMT
Date: Thu, 04 Apr 2002 11:34:01 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Thu, 14 Mar 2002 12:06:30 GMT
ETag: "0a7ccac50cbc11:1aad"
Content-Length: 52282

Examples of HEAD requests are also here: http://www.jibbering.com/2002/4/httprequest.html

And that page has the one you're looking for:

xmlhttp.open("HEAD", "/faq/index.html",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alert("File was last modified on - "+
    xmlhttp.getResponseHeader("Last-Modified"))
  }
 }
 xmlhttp.send(null)

You'd need to format the date and time... but there you have it.

Community
  • 1
  • 1
  • Your idea is good, but can i able to find what data is updated atlast... Because i have to fetch that content alone.... –  Sep 18 '09 at 05:49
  • What I've outlined is a method to determine whether a file has been updated and then act upon it... I cannot tell you which data within the file has been updated, for that you will need to think about the data in the file being stored in such a way that you are able to make such determinations. For example you could include a timestamp each time you write to the file, and then show all lines that occur within the last 5 minutes. Or you could show the last X lines so that it is equivalent to *nix's "tail -f" –  Sep 18 '09 at 07:14
0

No, you can't read a file content using JavaScript. JavaScript resides on the client and can't read server side file contents.

You can request the entire file using AJAX and get the response. By this you can request a server side page and get the file contents from there and check for any modification. And then pass a response to the client showing whether the file was updated or not.

rahul
  • 184,426
  • 49
  • 232
  • 263