Is it possible to retrieve the last modified date of a file using Javascript?
If it's on the same server as your calling function you can use XMLHttpRequest-
This example is not asynchronous, but you can make it so if you wish.
function fetchHeader(url, wch) {
try {
var req=new XMLHttpRequest();
req.open("HEAD", url, false);
req.send(null);
if(req.status== 200){
return req.getResponseHeader(wch);
}
else return false;
} catch(er) {
return er.message;
}
}
alert(fetchHeader(location.href,'Last-Modified'));
This seems to be useful, and works for me - giving you the 'local' date
document.lastModified
Compared to the above selection of req.getResponseHeader() it's one less round trip/http call.
Using the modern fetch
method:
var lastMod = null;
fetch(xmlPath).then(r => {
lastMod = r.headers.get('Last-Modified');
return r.text();
})