Sunday, November 8, 2009

document.lastModified in Google Chrome

It's fairly common to put something like document.write(document.lastModified); on a web page to display the date and time the document was last updated. This works fine in IE and in FireFox and seems like straight forward cross browser compatible and is kosher according to "JavaScript: The Definitive Guide, 5th ed."

Chrome doesn't like it. Now, for cross browser compatibility, you've gotta use
document.write(Date().toLocaleString(document.lastModified));


I'm not sure what's going on here. The Chrome JavaScript console shows document.lastModified as undefined. Maybe it's a WebKit thing. I'll have to see what Safari does.

Alright everyone... update your scripts!

2 comments:

Unknown said...

That's not the solution, if you refresh the browser in e.g. Safari it keeps updating the time even if no changes were made to the web page. With simply

document.write(document.lastModified);

it doesn't do that in FireFox.

RPG Market said...

You can use that to get the last modified when you first load the page, store it in a variable, and then use ajax to get the last-modified on the file and compare the two for determining whether to reload the page or not...