|
Visual Basic Script (VBScript)
Part:
1
2
3
(Continued from previous part...)
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.
Supprised? I was supprised before I learned that execution rule about script blocks
mentioned in the previous section. The output also tell 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.
Variables and Expressions in VBScript
Special rules about variables and expressions:
- Variables used without declarations are called "variant" variables.
- Variant variables can be used to store different data types.
- Variable names are case insensitive.
- "=" is the assignment operator. But it is also the boolean equality operator.
- "&" is the string concatenation operator.
Here is a sample ASP page, variable.asp:
<script language="vbscript" runat="server">
' variable.asp
' Copyright (c) 2002 by Dr. Herong Yang
' This program demonstrates some special rules about variables and
' expressions.
'
response.write("<html><body>")
response.write("<b>Tests on variables</b>:<br/>")
i = 1 + 2 + 3
s = "Sum = "
s = s & i & "<br/>"
i = i=6
response.write(s)
response.write("Is the answer correct? " & i)
response.write("</body></html>")
</script>
Output:
Tests on variables:
Sum = 6
Is the answer correct? True
"i = i=6" is a very unusual statement. First of all, variable "i" has changed its data type
from integer to boolean. Secondly, two "=" signs are used next to each other, the first
one is an assignment operation, and the second one is a boolean equality operator.
(Continued on next part...)
Part:
1
2
3
|