Hi Friends, 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..
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.
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://stackoverflow.com/questions/333634/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.