IntroductionTimestamping an HTML document that one has created conforms to a "good" style of HTML markup It gives the viewer a sense as to how old the document is. Perhaps, more importantly, this is of relevance to a returning user who may first wish to know if the document has been modified since he/she visited it the last time before browsing through. Many HTML editors in current use (such as Emacs with the html-helper-mode or Microsoft FrontPage 98) have a built-in capability of including-in the date of last modification automatically when one saves the document. In this article, we show two ways of how it can be done without the use of special-purpose HTML editors. Server-Side SolutionUsing Server-Side Includes (SSI's) This method uses SSIs. For this, an appropriate WWW server directive
should be enabled. (For example, Create a file called lastmodified.shtml with the following line:
Include the line
at the end of your HTML document in which you wish to include the last modification date and save it with extension .shtml. You can also introduce different colours and fonts in your lastmodified.shtml file to make the output more attractive. Using the flastmod directive, you can also inserts the last modification
date and time of other documents. (The difference between the
SSI environment variable
This can be useful if you have an index or table of contents page which not only lists what documents are available, but also when were they last modified, as the next example illustrates:
Using CGIIn dynamically generated HTML documents using CGI in Perl 5, you can insert a time stamp by including:
in your CGI script. Limitations Use of SSI's is not always recommended as it puts a load on the server
each time a file of the type Client-Side SolutionUsing JavaScriptTo use this method, the browser should be JavaScript-compatible (such as Netscape Communicator 2.0+ or Microsoft Internet Explorer 3.0+). Including the following segment of code in your HTML document does the work: Script 1.
This is simplest form of script that is possible. The above script will return the exact time of modification down to the hours, minutes and seconds. Variants of this script can make the output more "user-friendly". For example, if you wish to have an output as Month Date, Year (say July 4, 1998) only, it can be done by including the following segment of code in your HTML document: Script 2.
You can also introduce different colours and fonts to make the output more attractive. With a little bit of more work on the script, corresponding day and date suffixes (st, nd, rd, th) can also be added (so that the above example appears as Saturday, July 4th, 1998). By adding appropriate functions for the day prefixes and date suffixes to the Script 2, and modifying the output the final form of Script 2 then becomes: Script 2'.
External JavaScriptWith JavaScript 1.2-compatible browsers (such as Netscape Communicator 4.0+ or Microsoft Internet Explorer 4.0+), such scripts don't have to be included in the document directly and can be reused. All that is needed, is (a pointer to) the path to the file containing the script. However, you may have to configure your server and add a MIME type to your configuration in order to prevent it from returning a plain-text file. Including the following segment of code in your HTML document
where the file lastmodified.js contains any of the above portions of the scripts that are between the tags <SCRIPT LANGUAGE="JavaScript"><!-- and //--></SCRIPT>, and resides in the scripts directory. LimitationsUnfortunately, it seems that the JavaScript technique doesn't work if the file is already using SSIs (say for some other purpose). That is, the above technique will not work if the file is of the type *.shtml and includes
Changing TechniquesOne can use one of the above techniques or the other. Suppose we have a collection of files (over a file system) in which we previously used the SSI technique and want now to use the JavaScript technique. The task, after inserting the appropriate <SCRIPT> tag, then becomes of renaming these *.shtml files to *.html files. Using a Perl script this can be accomplished easily as follows: Step 1. Copy (though maintain the copyright) the following script to a file, call it rename and change the path: #!/usr/bin/perl to wherever you have Perl on your system.
Step 2. On the shell prompt (for UNIX platforms; on other platforms a variant of find should work) type the following where the directory_name is the top level directory (with subdirectories where *.shtml files are residing):
Of course, the same idea applies if you wish to rename *.html files to *.shtml files:
Y2K ComplianceIn the server-side approach, the compliance depends directly on the operating system and hardware on which the script is being run. In the client-side approach, use of getFullYear() assures the Y2K-compliance to a large extent, but requires (1) "appropriate" use of the Date() object, and (2) that the browsers support JavaScript 1.2 and above (to be precise, the ones that support ECMAScript as defined in ECMA-262 standard). The issue of Y2K-compliance in JavaScript will be addressed in detail elsewhere. ConclusionWith some basic knowledge of SSIs or JavaScript, you can add customized timestamps to your HTML documents. AcknowledgementsI would like to thank Alan Liu, and particularly Dave Campbell, for their suggestions regarding Y2K compliance in JavaScript scripts presented here.
If you our looking for support on computers try computer support they may be able to help.
|