|
Microsoft® Visual Basic® Scripting Edition Adding VBScript Code to an HTML Page |
VBScript Tutorial Previous | Next |
To create dynamic pages and CGI-based applications, you may take advantage of Microsoft's Active Server Pages (ASP) technology. ASPs are based on VBScript, Microsoft's development language. To embed VBScript in an HTML page, all you need to do is create a HTML file with the extension ".asp". ASP files are processed by our server prior to sending the page to the browser, which makes it very easy to create dynamic pages. To embed VBScript within an ASP page, you simply enclose the commands within an opening <% symbol and a closing %> symbol. This is known as a SCRIPT element.
Note that writing VBScript in this manner is called server-side scripting, where processing occurs at the server. There is also a way to refer to VBScript that is called client-side scripting. Client-side scripting is also a very popular approach to creating interactive pages. However, for client-side scripting we recommend using JavaScript, which is more universally supported. Nevertheless, using VBScript for server-side scripting, as described here, is very powerful and compatible with any browser.
VBScript code is written within paired <% ... %> tags. For example, a procedure to test a delivery date might appear as follows: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:<% Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function %>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> <% Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function %> </HEAD> <BODY> ...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.