Mixing VBScript Statements with Static HTML Text

This section provides tutorial examples on how to mix 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.

In the next example, I will show you the difference between in-line scripts and script blocks:

<!-- output_sequence.asp
     Copyright (c) 2002 by Dr. Herong Yang
     Verifying output sequence of scripts and HTML text. 
-->
<html><body>
<script language="vbscript" runat="server">
   response.write("Text line 1.<br/>")
</script>
Text line 2.<br/>
<script language="vbscript" runat="server">
   response.write("Text line 3.<br/>")
</script>
Text line 4.<br/>
</body></html>

Output:

Text line 2.
Text line 4.
Text line 1.
Text line 3.

Surprised? I was surprised before I learned that execution rule about script blocks mentioned in the previous section. The output also tells us that the default language on IIS 5.0 is set to VBScript. Otherwise, text line 1 and 3 would be displayed first. The problem can be avoided by using the in-line scripts:

<%@ language="vbscript"%>
<!-- output_sequence_corrected.asp
     Copyright (c) 2002 by Dr. Herong Yang
     Verifying output sequence of scripts and HTML text. 
-->
<html><body>
<%
   response.write("Text line 1.<br/>")
%>
Text line 2.<br/>
<%
   response.write("Text line 3.<br/>")
%>
Text line 4.<br/>
</body></html>

Now you know the different ways to use VBScript in ASP pages. But which way is better?

In-line script, <%...%>, is very flexible in mixing dynamic data with static HTML text. But the syntax is not so XML friendly.

Script block, <script ...>...</script>, is not useful at all if you want to keep static HTML text in your page. It can only be used, if you want to write the entire page in a single block, and output all HTML text with response.write() calls.

Personally, I prefer script block syntax, because I could easily manipulate it with XML tools. So I will be using script blocks most of the time for my sample ASP pages in this book. Note that even HTML comment tag <!-- --> must be moved into the script blocks.

Last update: 2003.

Table of Contents

 About This Book

 ASP (Active Server Pages) Introduction

 IIS (Internet Information Services) 5.0

 MS Script Debugger

VBScript Language

 Using VBScript in ASP Pages

Mixing VBScript Statements with Static HTML Text

 Variables and Expressions in VBScript

 Arrays and Loops in VBScript

 "Collection" Class in VBScript

 ASP Built-in Run-time Objects

 ASP Session

 Creating and Managing Cookies

 Managing Sessions with and without Cookies

 scrrun.dll - Scripting Runtime DLL

 Managing Response Header Lines

 Calculation Speed and Response Time

 ADO (ActiveX Data Object) DLL

 Working with MS Access Database

 Guest Book Application Example

 References

 PDF Printing Version