Next page | Contents page |

Exercise 2 - Date & time

A word of warning

Browsers tend to cope with incorrect HTML but they are much more fussy about JavaScript. Take great care to type the program examples exactly as shown. Pay attention to letter cases, spacing and punctuation. If it doesn't work, that kind of thing should be checked first.

Start a new file in your text editor:

  1. Type in this JavaScript code (it will be explained soon):
    
    function showDateTime ()
    {
      var now = new Date ();
      document.write ("It is now " + now);
    }
    
  2. Save this as a file called datetime.js in the same directory as your index.html

Then edit your index.html file from the first exercise:

  1. Insert a script tag in the head section to load JavaScript from a file called datetime.js
  2. Modify the starting tag of the body element to add this attribute:
    
      onload="showDateTime()"
    
  3. Save the file again (still called index.html).

Open the new version of index.html in your browser. In most browsers you should see something like this:

However, if you are using Microsoft's Internet Explorer (IE) you are more likely to see this:

Microsoft's reason for this is that an HTML page displayed from disc may have been saved there from a web site and therefore may pose a security threat if it contains any kind of program (not just HTML). In our case the warning is just an irritation that is thwarting our attempt to run a simple program.

Fortunately there is a simple solution. Enter this as a new line at the start of index.html, before the start of the html tag:


<!-- saved from url=(0014)about:internet -->

From now on include that line before the opening html tag for every HTML page you write and everything will work in IE without unnecessary warning messages.

Microsoft Obstructs The Way

More about that extra line, which is known as MOTW (the Mark Of The Web), can be found on microsoft.com, here.

What if it still doesn't work?

See the next page.

Next page | Contents page |