Date Last Modified
May 13, 2002
Here's a script that tells visitors when the page they are looking
at was last updated. It retrieves the Last Modified date from the
properties of the HTML file and then displays it. This script is
generally placed near the end of a page
<script language="JavaScript">
<!--
var LastUpdated = document.lastModified;
document.writeln ("Page last updated " + LastUpdated);
// -->
</script>
Like the frame-breaker script, this works automatically without
any kind of trigger code. Notice that the text "Page last
updated" is written using JavaScript rather than plain HTML.
The reason is that any browser that doesn't understand JavaScript,
or has it turned off, will omit the entire line, rather than
showing "Page last updated" with no date to follow.
If you use a script that will leave messy debris if the JavaScript
can't be read, or is vital to the functioning of your page, then
you need to use the <noscript> tag to explain to puzzled
visitors why they're seeing a mess. For example:
<noscript>
This page requires JavaScript, which is not enabled on your browser.
</noscript>
This message will only be displayed on browsers that can't
understand JavaScript. You can also include a regular hyperlink
(<a href="alternativepage.htm">Alternative page</a>)
within the noscript tags if you want to suggest a different page
to your disappointed visitor. In fact any valid HTML code can be
used between noscript tags.
You can place the noscript tags anywhere within the body of your
page. You could put them at the top if the JavaScript is essential
to the entire page, or put them in the position where the missing
JavaScript is going to leave a gap.
One thing to bear in mind is that search engine spiders don't
understand JavaScript, which means they do read the text within
<noscript> tags. When the noscript is near the top of a
page it can sometimes finish up as the main expression indexed
by the search engine spiders, and you'll see it returned in
search engine results.
Current Date
Here's a third simple script that comes as one piece. Wherever
you place it on your page, it displays the current date (in the
center of the page):
<SCRIPT LANGUAGE="JavaScript1.2">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source! http://javascript.internet.com -->
<!-- Begin
var months=new Array(13);
months[1]="January";
months[2]="February";
months[3]="March";
months[4]="April";
months[5]="May";
months[6]="June";
months[7]="July";
months[8]="August";
months[9]="September";
months[10]="October";
months[11]="November";
months[12]="December";
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var year=time.getYear();
if (year < 2000) // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
document.write("<center>" + lmonth + " ");
document.write(date + ", " + year + "</center>");
// End -->
</SCRIPT>
This script takes up many lines and brings up an important issue
in JavaScript - the sanctity of linebreaks. The most common error
when copying and pasting scripts is losing linebreaks. If you get
a script that doesn't work, the first thing you should check is
that the linebreaks on your page are absolutely identical to the
linebreaks in the original. Some lines may contain nothing more
than a single bracket, but if you start combining these with
other lines, thinking that it looks neater, you'll find your
script won't work.
JavaScript for Non-Programmers
JavaScript for Non-Programmers
Rollovers
|