MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Extension: More complex functions

A good example of a function that uses more complex JavaScript features, such as arrays and objects (see later unit), is a function to perform the standard task of displaying the date which a Web page was last updated. For example a 'banner' can be displayed at the end of each document as follows:

The code to create such output is as follows:

      <HTML>  <SCRIPT> <!--
      //function called update()
      function update()
      {
      //declare a variable called
      // (Modified to equal date of last save)
      var modified = document.lastModified;
      var months = new
      Array("Jan","Feb","Mar","Apr","May","June","July","Aug",
      "Sept","Oct","Nov","Dec");
      //declare a variable called ModDate to equal last modified
      date
      var modDate = new Date( modified );
      //write string of html formatting
      document.write('<center><hr width=200><font size=2><b>');
      document.write('This page was last updated: ');
      //write day
      document.write( modDate.getDate() + '/');
      //write month
      document.write( months[ modDate.getMonth() ] + '/');
      //write year
      document.write(modDate.getYear());
      //write string of html formatting
      document.write('</b></font><br><hr width=200></center>');
      }
      //invoke function
      update()
      // --> </SCRIPT> </HTML>
    

You may wish to examine this function now, and perhaps revisit it after working through the arrays and objects unit later in this module.