1

I have a webpage (i.e., a single-page website) and I want to add a script showing the last time the page was updated (by me), that is, the last time the source HTML file changed.

If I use this code inside the HTML document

document.write(document.lastModified)
Last updated on

I get the current date (I mean, each time I refresh the page, the date changes), I guess because actually the file is being changed by the script.

So, what can I do??

Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
Vicent
  • 313
  • 1
  • 3
  • 15
  • 1
    I get the same behaviour with ``` Last updated on . ``` – Vicent Jul 30 '19 at 20:02

1 Answers1

4

It seems that the lastModified method is spotty at best. This method only works for truly static sites since dynamic sites are generating the HTML page when requested.

Even then, it seems that the lastModified method just returns the current date when the last modified date is unknown. Mozilla's documentation makes this very clear:

Files without a known last modified date return the current date. - MDN Web Docs

If you are not able to programmatically produce your last modified date and you're just working with a single page static site, it may be less headache to just hard-code this information into the page.

Jacob Helton
  • 173
  • 9
  • Yes... I wanted to avoid that, but maybe that's the simplest solution. – Vicent Jul 30 '19 at 20:15
  • 1
    If you're working with a single static file, I'd say likely so. Especially if you are the only author/editor of the page. It may also be worth knowing how your hosting provider serves their files, that might provide a little more insight to whether it's a hosting problem or a page problem. – Jacob Helton Jul 30 '19 at 20:29