2

Is it possible? Now, I have done live chat, where with jquery's help I connect to .php file and check last modified time and if it is not as before, I retrieve messages. If it were possible in javascript I probably would save a lot of resources.

Thanks.

good_evening
  • 21,085
  • 65
  • 193
  • 298
  • If it's possible with jQuery then it's possible in plain old Javascript. Can you share what you've done with jQuery? – Jonathan Apr 21 '11 at 15:55
  • But the client never actually receives the php file, they really receive the output of running that file. I have my doubts the client can determine this, but I'll watch this question with interest. – Matt Greer Apr 21 '11 at 15:56
  • @Jonathan: I use jQuery's ajax and use .php file for that. – good_evening Apr 21 '11 at 15:58
  • i was thinking using ajax to get `getResponseHeader('Last-modified')` – KJYe.Name Apr 21 '11 at 16:02

3 Answers3

13

It's definitely possible if the server is sending an accurate Last-Modified header for that particular file:

var getMTime = function(url, callback) {
  var xhr = XMLHttpRequest();
  xhr.open('HEAD', url, true); // use HEAD - we only need the headers
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      var mtime = new Date(xhr.getResponseHeader('Last-Modified'));
      if (mtime.toString() === 'Invalid Date') {
        callback(); // dont want to return a bad date
      } else {
        callback(mtime);
      }
    }
  }
  xhr.send();
};

getMTime('url here', function(mtime) {
  if (mtime) console.log('the mtime is:' + mtime.toISOString());
});
chjj
  • 14,322
  • 3
  • 32
  • 24
  • `var xhr = XMLHttpRequest();` failed for me in chrome, so I did it with `$.ajax` and the method worked there. +1 for a useful answer needing only slight modification. – Maslow Jan 29 '13 at 18:00
1

Short answer: there's no way but AJAX + a server-side script (in your case, jQuery + php)

Being a client-side script, javascript gets run on the client's computer, so if the file whose m-time you want to check is on the server, then you are correct to use AJAX and a server-side script. No other way will work.

If the file whose m-time you want to check is on the client's computer, then you're out of luck. Javascript is intentionally designed to be prevented from accessing the client's files. (It can only access cookies, which are on the client's computer, however, because the browser (not any javascript) loads those into its work environment.)

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
  • 1
    This isn't true anymore, you can access local files with the new FileReader API that's part of HTML5. You need the user to select the file for you however, you can't just start sniffing around their system. – Matt Greer Apr 21 '11 at 16:11
  • Ah, true enough. It didn't occur to me that the coder would be checking the meta data of such a file. – JellicleCat Apr 21 '11 at 19:23
0

Maybe HTTP ETag headers could be used to check if the page has changed. The first response contains ETag and your client uses that for the following request. Your PHP server side code would then send 304 if the page has not been modified.

http://en.wikipedia.org/wiki/HTTP_ETag

Petteri H
  • 11,779
  • 12
  • 64
  • 94