Example HTML and JavaScript to automatically
print on your page when it was last modified
Conventions: When a portion of code is a different color,
the text that is of the same color references that code.
"This page" and any relevant files can be downloaded on the previous
page by clicking the green disk next to the link that brought you to this
page.
Test this page
Try making any modifications to this page. Then save it. Run
it in your browser and the "last modified" date will be updated
too.
The HTML portion
To see how it works, study the information between the SCRIPT and /SCRIPT tags in the HTML. This line tells the browser to load a separate script file (lastupdate.js) into the page and runs it at the point in the page where the SCRIPT tag exists.
<SCRIPT type="text/javascript" language = "Javascript" src="lastupdate.js"></SCRIPT>
Tells the browser that a script is
to be interpreted, end of script interpretation
Tells newer and older browsers that
the user's browser should interpret the script as JavaScript (and not VBScript,
JScript, etc.)
Tells the browser where to load the script file from.
You could simple embed the script file into your HTML but that would: 1. be
more difficult to modify 2. be more difficult to understand 3. allow the user
to see the "source" of the script, something you may not want to
do.
The JavaScript portion
Load the lastupdate.js file (you can use Notepad) to see the code. This is file pure JavaScript and tells the browser to insert the text and the 'last modified' date into the page.
document.write("<b><font
color='#CCCCCC' size='2'> <font color='#0080C0'>this page last modified
</font></font></b>");
document.write("<font
color='#0080C0'><strong><font size='2'>");
document.write(document.lastModified);
document.write("</font></strong><br></font>
</p>");
In JavaScript, anything between the parenthesis
and double quotes in a document.write statement will write to the document.
You can even write HTML code to the page, as shown here.
All of this is normal HTML code, and formats the text
that is being written
This is the line that does it all. The 'document' is
your page. The document is called an object. Each 'item' on your page is also
an object. Objects have properties (characteristics) and methods (ability
to do carry out an action). In this line, we are using the document method
'document.write' to write the document's 'lastModified'
property to the page.
Notes on all JavaScript scripts:
upper and lower case is IMPORTANT!
you SHOULD ALWAYS end each statement with a semicolon (;). Some browsers do
not require them, but you will find that if you do not use them, you will
run into hard-to-detect errors as well as confusing some browsers.