Microsoft® Visual Basic® Scripting Edition
Adding VBScript Code to an HTML Page
VBScript Tutorial |
Previous | Next
You can use the SCRIPT element to add VBScript code to an HTML page.
VBScript code is written within paired <SCRIPT> tags. For example, a procedure to test a delivery date might appear as follows:Beginning and ending <SCRIPT> tags surround the code. The LANGUAGE attribute indicates the scripting language. You must specify the language because browsers can use other scripting languages. Notice that the CanDeliver function is embedded in comment tags (<!-- and -->). This prevents browsers that don't understand the <SCRIPT> tag from displaying the code.<SCRIPT LANGUAGE="VBScript"> <!-- Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function --> </SCRIPT>
Since the example is a general functionit isn't tied to any particular form controlyou can include it in the HEAD section of the page:You can use SCRIPT blocks anywhere in an HTML page. You can put them in both the BODY and HEAD sections. However, you'll probably want to put all general-purpose scripting code in the HEAD section in order to keep all the code together. Keeping your code in the HEAD section ensures that all code is read and decoded before it's needed by any calls from within the BODY section.<HTML> <HEAD> <TITLE>Place Your Order</TITLE> <SCRIPT LANGUAGE="VBScript"> <!-- Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function --> </SCRIPT> </HEAD> <BODY> ...
One notable exception to this rule is that you may want to provide inline scripting code within forms to respond to the events of objects in your form. For example, you can embed scripting code to respond to a button click in a form:Most of your code will appear in either Sub or Function procedures and will be called only when code you have written causes it to execute. However, you can write VBScript code outside procedures, but still within a SCRIPT block. This code is executed only once, when the HTML page loads. This allows you to initialize data or dynamically change the look of your Web page when it loads.<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>