Entry
How can I display a last modified date for a link?
Feb 11th, 2001 05:05
Martin Honnen,
Note first that client side js is rather inadequate to solve this task
as for every link a server access is necessary. The task is much better
solved with SSI (server side includes) or server side scripting. If you
don't have access to server side methods the following code works for
NN4 using java (of course only for links on the same server as your
page containing the links) and for IE5+ using the Microsoft.XMLHTTP
object. While the NN4 code works locally (with file: url) and on the web
(with http: url) the IE5 code works only on the web, i.e. you have to
upload the file to a web server.
<HTML>
<HEAD>
<SCRIPT>
function getLastModifiedURL (url) {
if (document.layers && navigator.javaEnabled()) {
var url = new java.net.URL(url);
var conn = url.openConnection();
return new Date(conn.getLastModified());
}
else if (document.all && document.getElementById && !window.opera) {
var http = new ActiveXObject('Microsoft.XMLHTTP');
http.open('HEAD', url, false);
http.send();
var lastModifiedHeader = http.getResponseHeader('Last-modified');
return new Date(lastModifiedHeader);
}
else
return null;
}
function writeLastModLink (url, text) {
var html = ''
html += '<A HREF="' + url + '"';
html += ' ONMOUSEOVER="if (!this.lastMod) ' +
'this.lastMod = new LastModifiedObject(this.href);';
html += ' var lm = this.lastMod.getLastModified(); if (lm) ' +
' window.status = \'Last modified: \' + lm; return true;"';
html += ' ONMOUSEOUT="window.status = \'\'; return false;"';
html += '>';
html += text ? text : url;
html += '</A>';
document.write(html);
}
function LastModifiedObjectGetLastModified () {
if (document.layers || (document.all && document.getElementById &&
!window.opera)) {
if (!this.lm)
this.lm = getLastModifiedURL(this.img.src);
return this.lm;
}
else if (document.all)
return this.img.fileModifiedDate;
else
return null
}
function LastModifiedObject (url) {
this.url = url;
this.getLastModified = LastModifiedObjectGetLastModified;
this.img = new Image();
this.img.object = this;
this.img.src = url;
}
var lmo = new LastModifiedObject('somePage.html');
</SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript: alert(lmo.getLastModified()); ">
show
</A>
<UL>
<LI>
<SCRIPT>
writeLastModLink('whatever.html', 'a link');
</SCRIPT>
</LI>
<LI>
<SCRIPT>
writeLastModLink('whatelse.html', 'a 2nd link');
</SCRIPT>
</LI>
</UL>
</BODY>
</HTML>
The above code also contains experimental code for IE4 using the Image
object to try to get a the link last modified date. This code however
doesn't seem to provide the correct date when used in a http: context.
It only works when used locally (with file: urls).