Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

lastModified date of other HTML files 1

Status
Not open for further replies.

retrositelover

Programmer
Oct 20, 2004
35
0
0
Hello,

I'm still very new to Javascript and have a rather kind of beginner question:

for a single HTML file, I love to use this in order to give out the file date: document.write(document.lastModified);

now, I've got an HTML page which links all the logfiles of my different online projects: would be a dream to have a possibility listing the last edited date beneath each link

is there any chance to do this with Javascript? I can't use server side languages for my webhost doesn't support any. the only way I know would be to place Iframes embedding a small portion of each linked file where I could place the above snippet in a layer, but I'm wondering if there is any chance to read out the date of another file the client sided way and to write it down in another HTML page

thanks in advance + nice greetings
J.
 
Hi

While JavaScript runs on client side, it can not get the last modified date of documents on the server. But can request them, and get the [tt]Last-modified[/tt] HTTP header's value. In big image, it is similar to your [tt]iframe[/tt] idea.
Code:
function getlastmod(what)
{
  var http=new XMLHttpRequest()
  http.open('HEAD',what,false)
  http.send(null)
  if (http.status!=200) return undefined
  return http.getResponseHeader('Last-modified')
}
Code:
<ul>
<li>first.html <script type="text/javascript">document.write(getlastmod('first.html'))</script></li>
<li>second.html <script type="text/javascript">document.write(getlastmod('second.html'))</script></li>
<li>third.html <script type="text/javascript">document.write(getlastmod('third.html'))</script></li>
</ul>
Note that
[ul]
[li]Not all browsers have [tt]XMLHttpRequest[/tt], but you can find wrappers for them on the web. I kept the example simple.[/li]
[li]Not all web servers know the [tt]HEAD[/tt] request method, if you have problems with your, change it to [tt]GET[/tt].[/li]
[li]I used synchronous request, because it is easier to handle in this case, but slows down the page loading. Changing it to asynchronous would mean an almost complete rewrite of the code.[/li]
[/ul]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top