The "last updated" script automatically resets the displayed date on a web
page each time the html source document is modified in any way.
This script consist of only two lines of JavaScript code:
var LastUpdated = document.lastModified;
document.writeln ("This page was last updated " +
LastUpdated);
The Structure:
The JavaScript code goes into to the body of the html document at
the point where it is to be displayed. It is inserted within a pair of html
<script> tags. The opening script tag includes the attribute
language="JavaScript" to distinguish it from other possible scripting
languages (such as JScript or VBScript). The code is embedded
within html comment tags (<!-- ...... -->), to hide it
from older browsers that
can't interpret JavaScript Code - otherwise the code will show up on the
web page when viewed by non-JavaScript enabled browsers. JavaScript
comments are preceded by two slashes //. Upper Case is used for the html
tags and lower case is used for most of the JavaScript code. The former
convention is optional, while the latter is required; JavaScript is "case
sensitive."
Here is the entire document structure with the JavaScript syntax highlighted:
|
<html> <head></head> <body> <script language="JavaScript"> <!-- Hide JavaScript... var LastUpdated = document.lastModified; document.writeln ("This page was last updated " + LastUpdated); // End Hiding --> </script> </body> </html> |
The Code:
I'll start with a simpler version of the code... The same code could have
been written in one line, like this:
"document.writeln( )" is a predefined Javascript
method
that writes whatever is within the parentheses onto the displayed web
page. The portion within the quotation marks can contain any user-defined
value such as "Last Updated" or "Last Modified".
"document.lastModified" is a JavaScript property
which evaluates to
the date and time that the html source document was last opened and
altered.
" + " is an operator which is used to
connect or "concatonate" the two values within the parentheses.
Now for the two-line version of the code:
var LastUpdated = document.lastModified;
document.writeln ("This page was last updated " +
LastUpdated);
The first line of this version is a variable
statement. " var " signals that a new user-defined
variable is being created.
LastUpdated is an arbitrary (user-defined) name for the variable,
essentially it is used as a shortcut for the "document.lastModified"
property in the subsequent statement.