ASP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.11

Visual Basic Script (VBScript)

Part:   1  2  3 

ASP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

hyBook - Guestbook Application

Using MS Access Databases

ActiveX Data Object (ADO)

Controlling Response Header Lines

Microsoft Scripting Runtime DLL

Using Cookies

ASP Sessions

ASP Objects

Microsoft Script Debugger

Internet Information Services (IIS)

... Table of Contents

Using VBScript in ASP Pages

There are three ways to use VBScript in ASP pages:

1. In-line VBScript statements: The VBScript statements must be enclosed in the special in-line script tag: <% %>. The enclosed statements will be executed by the server, and the output of response.write() calls will be merged in-line with the surrounding static HTML text. In-line VBScript statements require a language declaration statement to define explicityly VBScript as the server side scripting language, if you are not sure what is the default language defined on the Web server.

<%@ language="vbscript"%>
...
<%
vbscript_statement
vbscript_statement
...
%>

2. In-line VBScript expression: The VBScript expression must be enclosed in the special in-line script tag: <% %> with a leading "=" sign. The enclosed expression will be evaluated, and the resulting value will be converted into a string and merged in-line withh the surrounding static HTML text. In-line VBScript expression also require a language declaration statement to define explicityly VBScript as the server side scripting language, if you are not sure what is the default language defined on the Web server.

<%@ language="vbscript"%>
...
<%
= vbscript_expresion
%>

3. Script block of VBScript statements: The VBScript statements must be enclosed by the starting script block tag <script> and the ending tag </script>. Two attributes are required on the script tag: "language" and "runat".

<script language="vbscript" runat="server">
vbscript_statement
vbscript_statement
...
</script>

Please note that the script block VBScript statements will be executed by the server. But the output of response.write() calls will not be merged in-line with the surrounding static HTML text. It will be:

  • Inserted at the beginning of the HTTP reponse before any static HTML text, if the server's default scripting language is not VBScript.
  • Appended at the end of the HTTP reponse after any static HTML text, if the server's default scripting language is VBScript.

This rule was a big supprise to me. I had to post a message to microsoft.public.inetsdk.programming.scripting.vbscript, and got confirmed from Microsoft. See the article Using VBScript and JScript on a Web Page.

Mixing VBScript Statements with Static HTML Text

Here is an ASP page example to show you how in-line statements and expressions can be mixed with static HTML text:

<%@ language="vbscript"%>
<!-- inline_syntax.asp
     Copyright (c) 2003 by Dr. Herong Yang
     This program shows how to use inline scripting statements and 
     expressions.
-->
<html><body>
Good 
<% if 0 <= hour(time) and hour(time) < 12 then %> 
moring,
<% elseif 12 <= hour(time) and hour(time) < 18 then %>
afternoon,
<% else 
      response.write("evening,")
   end if %>
Herong.</br>
<% response.write("The current server time is: ") %>
<% = Time %> on <% = Date %>
</body></html>

Output:

Good evening, Herong.
The current server time is: 10:01:56 PM on 5/24/2003 

In the previous example, I was able to break the "if" statement into two parts and use in-line script for one part and static text for the other.

(Continued on next part...)

Part:   1  2  3 

Dr. Herong Yang, updated in 2003
ASP Tutorials - Herong's Tutorial Notes - Visual Basic Script (VBScript)