By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Javascript - lastModifed

P: 1
Hi,
I have a web page with links to external files and I would like to include the date the external file was last modified in the href description. Our servers don't seem to support SSI or PHP.

Any ideas how to do it? It feels as though it should be fairly simple but I can't find anything on the web.

Cheers

Doug
Oct 11 '07 #1
Share this Question
Share on Google+
2 Replies


epots9
Expert 100+
P: 1,351
Hi,
I have a web page with links to external files and I would like to include the date the external file was last modified in the href description. Our servers don't seem to support SSI or PHP.

Any ideas how to do it? It feels as though it should be fairly simple but I can't find anything on the web.

Cheers

Doug
jaascript is client side scripting and it can't read/write to files...unless you use ajax.
Oct 11 '07 #2

100+
P: 428
Using javascript is an expensive way to do this- and it only works for files on the same domain as the page with the links.

Much better to write a script on the server that will rewrite the html page
when you modify one of its linked files.

This script will add the modified date of a link to its title the first time you hover or focus it-
You can modify it to write all the modified dates into the link text in the onload handler,
but this really belongs in a server routine.
Expand|Select|Wrap|Line Numbers
  1.  
  2. function titleModified(who){
  3.     var aj= AJX(); // http request object, defined below (or use your own)
  4.     var url, t;
  5.     who= window.event? event.srcElement: who.target;
  6.     url= who.href;
  7.     t= who.title || '';
  8.     try {
  9.         aj.open("HEAD", url, false);
  10.         aj.send(null);
  11.         if (aj.status== 200) tem= aj.getResponseHeader('Last-Modified');
  12.         else tem= '';
  13.     }
  14.     catch (er) {
  15.         tem= '';
  16.     }
  17.     if(tem) who.title= t+' Last modified:'+tem;
  18.     who.onmouseover= ''; // don't call again for this element
  19.     who.onfocus= '';
  20. }
  21.  
  22. function AJX(){
  23.     var obj;
  24.     if (window.XMLHttpRequest) return new XMLHttpRequest();
  25.     else if (window.ActiveXObject){
  26.         try{
  27.             obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
  28.         }
  29.         catch(er){
  30.             try{
  31.                 obj= new ActiveXObject("Microsoft.XMLHTTP");
  32.             }
  33.             catch(er){
  34.                 obj= false;
  35.             }
  36.         }
  37.     }
  38.     return obj;
  39. }
  40.  
  41.  
  42. window.onload= function(){
  43.     if(!AJX()) return false;
  44.     var cnt= 0, who,tem, A= document.getElementsByTagName('a');
  45.     while(A[cnt]){
  46.         who= A[cnt];
  47.         tem= who.href;
  48. //next line to skip page bookmarks
  49.         if(tem && !/^#/.test(tem) && tem.indexOf(location.pathname)== -1){         
  50.             who.onmouseover= titleModified;
  51.             who.onfocus= titleModified;
  52.         }
  53.         ++cnt;
  54.     }
  55. }
Oct 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.