The Time is Now!
Up
One of the simplest possible scripts is the one which gives the current date and time like this:

 

Here's the code:

<script LANGUAGE="vbscript">
document.write Now
</script>


The above code writes the date and time to the page (the document) once. It doesn't update it - though a more complicated script could do this of course.

Before we go any further, let's see the Javascript equivalent would be for the above code:

<SCRIPT LANGUAGE=JAVASCRIPT>
document.write(Date()+".")
</SCRIPT>

And here is the result:

I hope you agree that the Javascript code is less easy to understand than the VBscript. Javascript is also case-sensitive which makes it even more difficult to write, but we must remember that Javascript has one big advantage - it runs on nearly all browsers whereas VBscript usually only runs on Internet Explorer.

From now on we'll concentrate on VBscript - let's look at a slightly more sophisticated piece of VBscript that allows you to update the time by clicking a button:

The time is:   

The script below simply displays the current time in the textbox named 'timeinfo'. The time value is updated when the user clicks the command button named cmdUpdate.

<script LANGUAGE="vbscript">
Sub btnTime_OnClick
frmMyForm.timeinfo.value = Now
End Sub
</script>

This time, the controls are contained within a form - in this case the name of the form must be included when the script refers to the control, since there may be several forms on the same page. Hence the lines that say  
    frmMyForm.timeinfo.value = Now
instead of just 
    timeinfo.value = Now


Up Next