Share via


Adding VBScript Code to an HTML Page

To add VBScript code to an HTML page, enclose the script within a pair of <SCRIPT> tags, using either the TYPE attribute or the LANGUAGE attribute to specify the scripting language. You can place VBScript code blocks anywhere on an HTML page. In fact, it is a good practice to put all general-purpose scripting code (that is, script that is not tied to a particular form control) in the HEAD section. This ensures that script will already have been read and decoded before it is called from the body of the HTML page.

The following code example shows a VBScript procedure to test a delivery.

<SCRIPT TYPE="text/VBScript"> 
'Or: <SCRIPT LANGUAGE="VBScript" 
<!--
   Function CanDeliver(Dt)
      CanDeliver = (CDate(Dt) - Now()) > 2
   End Function
-->
</SCRIPT>

Note that the CanDeliver function is embedded in comment tags (<!-- and -->). This prevents browsers that do not support the <SCRIPT> tag from displaying the code.

A script that is likely to be called more than once while a page is loaded will usually use a Function or Sub routine, which is then called from outside the script. Scripts that are called only once when a page is loaded, for example to initialize data or change the look of a page, do not use routines.

If you have a script that executes in response to a specific event within a particular form, you may prefer to embed the script in the form block. The following example uses VBScript code to respond to a button click in a form.

<HTML>
<HEAD>
<TITLE>Test Button Events</TITLE>
</HEAD>
<BODY>
<FORM NAME="Form1">
   <INPUT TYPE="Button" NAME="Button1" VALUE="Click">
   <SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript">
      MsgBox "Button Pressed!"
   </SCRIPT>
</FORM>
</BODY>
</HTML>

 Last updated on Saturday, April 10, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.