VBScript
VBScript is a subset of Visual Basic
developed by Microsoft. VBScript is the default scripting language of ASP. The functions of VBScript on the server
used with ASP is limited. VBScript is also scripting language of a Microsoft Server.
Although VBScript is
well supported by Internet Explorer, the level of cross-browser compatibility of
VBScript is low, because VBScript is not a standard HTML language. In other words, VBScript can be considered as a server scripting language.
VBScript Code
In general, the applications of VBScript include client-side scripting in
Internet Explorer, server-side scripting in Internet Information Services (IIS),
and Windows scripting in Windows Script Host. VBScripts of these applications
are of different features and specifications. For the client-side scripting in
Internet Explorer, all scripts are placed within the <script
type="text/vbscript">…</script>. For server-side scripting in ASP of IIS, all
scripts are placed within the delimiters <%…%> or the <script
language="vbscript" runat="server">…</script> element. For Windows scripting,
script is a plain text file with extension of ".vbs". And only the
vbscript for the server-side scripting is related to ASP scripting.
Server-Side VBScript Code
For server-side VBScripting, VBScripts inside a web page are interpreted through the ASP engine on the web server. There are two ways to add VBScripts in a web page;
Example of VBScript
Example of embedded Server Side VBScript tag, Integrated ASP script block and embedded Client Side VBScript tag for IE10.
HTML Document Input:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Page</title>
<meta http-equiv="x-ua-compatible" content="IE=10" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<h1>A Web Page Sample</h1>
<p><%="A client side VBScript for IE10 inside a server side VBScript on "&Now%></p>
<%Response.Write clientscript%>
<script runat="server" language="VBScript">
Function clientscript
clientscript=("<" & "script language=VBScript type=text/vbscript>"&vbCrLf)
clientscript=clientscript&(" a=MsgBox(""test msg"")"&vbCrLf)
clientscript=clientscript&(" document.write ""<p>Test Msg: ""&a&"&"""</p>"""&vbCrLf)
clientscript=clientscript&(" <" & "/script>"&vbCrLf)
End Function
</script>
</body>
</html>
ASP and Server-Side VBScripting
Since VBScripts in ASP page will be interpreted through the ASP engine on the web server, no user input and output interface are needed. Therefore the user input and output interface of VBScript features are disabled and the InputBox and MsgBox elements of VBScript are not supported in server-side VBScripting.
Besides, the output of server side VBScript is also manipulated through the ASP engine. The "Response.Write" method of the server Object is used to write the VBScript output to the HTML document before responding to the HTTP request. While the VBScript "Document.Write" method is used as client side VBScript for writing the output of VBScript to the HTML page, the Document object, by the Internet Explorer browser scripting engine before rendering on the display dervice.
In server-side VBScripting, although, both the ASP technology and the VBScript
language provide the "CreateObject" Method or Function, however Objects created
by VBScript using the "CreateObject" or "GetObject" function or Method of
the VBScript language are not compatible with the Objects created by the
"Server.CreateObject" of ASP technology. Because the "CreateObject" or
"GetObject" function or Method cannot access the ASP built-in Objects of the
server. Only instance of Objects created by "Server.CreateObject" can be kept
tracked by the server and can participate in transactions. However when the
vbscript are working with the Admin Objects of web server or Java monikers, the
"CreateObject" method or function is still needed to be used.
VBScripting Reserved Keywords
Since server side VBScripts are integrated with ASP technology to generate dynamic HTML page, keywords of ASP and HTML are also keywords of VBScripting.
- Character "&" is the reserved keyword for indicating an HTML character entity in HTML. Replacing a literal & in a HTML web page with an HTML character entity "&" is always a good practice, except for those characters that should be replaced by escaped character "%26" beforehand.
- Characters "<" and ">" are the reserved paired keywords for indicating an HTML tag entity in HTML. characters "<" and/or ">" must be replaced with HTML character entities "<" and/or ">" when text bounded by characters "<" and ">>" pair may cause the browser to be treated as a HTML tag. However, replacing literals "<" and ">" in a HTML web page with HTML character entities ""<" and ">" is always a good practice.
- Character '"' is the reserved keyword for enquoting character string in HTML, ASP, and VBScript. Characters '"' must be replaced with HTML character entities """ when text bounded by a pair of characters '"' may cause the browser to be treated as a HTML character string. However, replacing literals '"' in a HTML web page with HTML character entity """" is always a good practice. But when the character '"' is used as string delimiter and nested in another string delimiter pair in ASP or VBScript, the string delimiter is repeated successively.
- Both "<%" and "%>" are the reserved keywords for indicating the beginning and end of a ASP block. Since the ASP engine always evaluate "<%" and "%> as reseved keywords and reacts accordingly, "<" and ">" must be replaced with HTML character entities ""<" and ">" accordingly when using "<%" and "%>" as literals in a HTML web page. However, when using "<%" and "%>" keywords in a nested script, these keywords must be divided into two parts when reading by the ASP engine.
Examples of VBScripting Keywords
Examples of VBScripting Keywords
ASP VbScript Command:Response.Write "<p>&amp; is the HTML character entity of &</p>"
Response.Write "<p>&lt; is the HTML character entity of <</p>"
Response.Write "<p>&gt; is the HTML character entity of ></p>"
Response.Write "<p>&quot; is the HTML character entity of "</p>"
Response.Write "<p>Repeated delimiter double quotation ("""") can be used to represent nested delimiter double quotation (") in ASP code or VBScript.</p>"
Response.Write "<p>""<%"" and ""%>"" are evaluated as literals.</p>"
Response.Write "<p>""<"&"%"" and ""%"&">"" are also evaluated as literals.</p>"
HTML Web Page In-line Output:& is the HTML character entity of &
< is the HTML character entity of <
> is the HTML character entity of >
" is the HTML character entity of "
Repeated delimiter double quotation ("") can be used to represent nested delimiter double quotation (") in ASP code or VBScript.
"<%" and "%>" are evaluated as literals.
"<%" and "%>" are also evaluated as literals.
VBScripting Syntax
VBScripts are also compatible with the standard ASP
scripting syntax as an ASP element in an ASP page.
- The delimiters "<%" and "%>" can be either on the same line with the scripts
<% Response.Write ("<h1>ASP Sample</h1>") %>
or on an individual line.<%
Response.Write ("<h1>ASP Sample</h1>")
%>
-
the space between the delimiter and script is not necessary because ASP engine
removes white space including spaces, tabs, returns, and line feeds from both
VBScript and JScript command of an ASP file. But for other scripting languages,
the ASP engine preserves white space so that scripting languages dependent upon
position or indentation can be interpreted correctly.
<%Response.Write ("<h1>ASP Sample</h1>")%>
-
Comment the script between the delimiters can be done by placing an
apostrophe(') at the beginning of a line. And it is better to place another
apostrophe(') at the end of a line for better compatibility with JScript. Unlike
the HTML comments and client-side scripting comments, the server-side comments
are removed by the interpreter during processing and no server-side comment will
be sent to the browser.
<% ' comment '%>
-
A line break can be used to indicate the end of a script command in both
VBScript and JScript command of an ASP file.
<%
Response.Write ("<h1>ASP Sample</h1>")
Response.Write ("<h2>ASP Statements</h2>")
%>
-
For a very long VBScript command, the command can go multiple lines by adding a
underscore at the end of the line with immediately preceded by a space and
immediately followed by a line terminator (carriage return).
<% Response.Write "<h1>ASP Sample"&_
"</h1>"
Response.Write <p>6 + _
7</p>%>
-
Multiple commands can also be put on a single line if these commands are
seperated by a colon.
<%
Response.Write ("<h1>ASP</h1>") : Response.Write ("<h2>Statement and Comment the VBScrip")%>
-
Comment the
VBScript between the delimiters can be done by placing an
apostrophe(') at the beginning of a line.
Unlike the HTML comments and client-side scripting comments, the server-side
comments are removed by the interpreter during processing and no server-side
comment will be sent to the browser.
<% ' comment %>
- Inline comment can be done by placing an apostrophe(') after the end of a VBScript command
except for the output directive expression.
<% Response.Write ("<h1>ASP</h1>")' comment %>
-
Multiple lines comment can be done by placing an apostrophe(') on the
beginning of each line.
<%' comment line 1
'comment line 2
'comment line 3 %>
-
VBScript is not case sensitive. Variable with name, "TempStr" is same as
variable with name "tempstr", the two names are refered to same variable. And
the "Date" function can also be named "date". Similarly,"response.write" can
also be used to refer to the ASP "Reponse.Write" object.
Server-Side VBScript Elements
The elements of the server-side VBScript Language may be grouped as following.
-
Basic: The fundament components of VBScript Language. e.g. Data Types, Variables, Constants
and Keywords.
-
Errors: The error catalogue of VBScript Language. e.g. List of VBScript run-time and syntax
errors
-
Functions: Build-in functions in VBScript Language
-
Methods: Build-in methods in VBScript Language
-
Objects and Collections: Build-in objects and collections in VBScript Language
-
Operators: Build-in operations in VBScript Language
-
Properties: Build-in properties in VBScript Language
-
Statments: Build-in statements in VBScript Language