| Introduction to VBScript |
|
| Introduction |
|
| VBScript is a scripting language. |
|
| VBScript was created by Microsoft to use either as a client-side scripting language for the Microsoft Internet Explorer (versions 3.0 and later) or as a server-side scripting language with the Microsoft Internet Information Server (versions 3.0 and later). |
|
| A primary advantage for using the server-side approach is that the VBScript is processed by the server before it is transmitted to the client. |
|
| In our VBScript tutorial you will learn how to write VBScript, and how to insert these scripts into your HTML files to make your web pages more dynamic and interactive. |
|
| VBScript Version 5.0 was released in 1999. Certainly, the most important new feature of Version 5.0 is the ability to create your own class objects. Other new features included like the Timer function, With statement, and regular expression searching using the RegExp and Match objects. |
|
| Before you continue you should have a basic understanding of the following: WWW, HTML / XHTML and the basics of building Web pages. |
|
|
|
|
|
| Introduction to VBScript |
|
| What is VBScript? |
|
• VBScript is a scripting language.
• A scripting language is a lightweight programming language.
• VBScript, is a scripting language that can be run client-side (i.e. via your web browser), or server-side (i.e. on the web server).
• VBScript is a light version of Microsoft's programming language Visual Basic .
• VBScript is the default language of Active Server Pages (ASP).
• VBScript may very well be the most important programming language for many Web-application. |
|
|
|
|
|
| Introduction to VBScript |
|
| VBScript Code in an HTML Document |
|
| When a VBScript is inserted into a HTML document, the Internet browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. |
|
<html>
<head>
</head>
<body>
<script type="text/vbscript">
document.write(" Learning VBScript from eBIZ Education!")
</script>
</body>
</html>
|
|
| And it produces this output: |
|
| Learning VBScript from eBIZ Education! |
|
| To insert a script in an HTML document, use the <script> tag. Use the type attribute to define the scripting language. |
|
<script type="text/vbscript">
|
|
| Then comes the VBScript: The command for writing some text on a page isdocument.write: |
|
| document.write("Learning VBScript from eBIZ Education! ") |
|
| The script ends: |
|
</script>
|
|
|
|
|
|
| Introduction to VBScript |
|
| Handle Older Browsers |
|
| Older browsers that do not support scripts will display the script as page content. To prevent them from doing this, you can use the HTML comment tag: |
|
<script type="text/vbscript">
<!--
Learning VBScript from eBIZ Education!
-->
</script>
|
|
|
|
|
|
| Introduction to VBScript |
|
| Placing the VBScript |
|
| Scripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event. |
|
| Scripts in the head section: Scripts to be executed when they are called or when an event is triggered go in the head section. When you place a script in the head section you will assure that the script is loaded before anyone uses it: |
|
<html>
<head>
<script type="text/vbscript">
some statements
…….
Learning VBScript from eBIZ Education!
</script>
</head>
|
|
| Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page: |
|
<html>
<head>
</head>
<body>
<script type="text/vbscript">
some statements
……….
Learning VBScript from eBIZ Education!
</script>
</body>
|
|
| Scripts in both the body and the head section: You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section. |
|
<html>
<head>
<script type="text/vbscript">
some statements
……….
Learning VBScript from eBIZ Education!
</script>
</head>
<body>
<script type="text/vbscript">
some statements
………
Learning VBScript from eBIZ Education!
</script>
</body>
|
|
|
|
|
|
|
| Working With VBScript |
|
| Adding VBScript to a Web page |
|
| In this example, you will create an HTML document and add a simple script to respond to a click event generated by a command button. You will need to be familiar with creating and testing an HTML document. |
|
| Creating the HTML Document |
|
| Open up a text editor application and insert the following HTML code: |
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Working With VBScript </TITLE>
</HEAD>
<BODY>
<H1>Your First VBScript Example</H1>
<P> By utilizing VBScript you can give your web pages actions.
Click on the button below to see what we mean. </P>
<FORM NAME="frmExercise1">
<INPUT TYPE="Button" NAME="cmdClickMe" VALUE="Click Me">
</FORM>
</BODY>
</HTML>
|
|
| Save the file and test it by loading it into Internet Explorer. The resulting page should be similar to the figure below. |
|
 |
|
| Try out the Click Me button. Does anything happen? |
|
| In the next part we will add a script to provide functionality for the Click Me command button. |
|
|
|
|
|
|
| Working With VBScript |
|
| Adding VBScript |
|
| Re-open the HTML document that you created in part 1, if necessary. Modify the document adding the lines shown with shading below: |
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Working With VBScript </TITLE>
</HEAD>
<BODY>
<H1>Your First VBScript Example</H1>
<P> By utilizing VBScript you can give your web pages actions.
Click on the button below to see what we mean. </P>
<FORM NAME="frmExercise1">
<INPUT TYPE="Button" NAME="cmdClickMe" VALUE="Click Me">
<SCRIPT FOR="cmdClickMe" EVENT="onClick" LANGUAGE="VBScript">
MsgBox "A simple example of VBScript for You." & Vbcrlf &
"You are Learning VBScript from eBIZ Education!"
</SCRIPT>
</FORM>
</BODY>
</HTML>
|
|
| Save the file and test it by loading it into Internet Explorer. Try out the Click Me button. The result is shown below: |
|
 |
|
| How It Works |
|
| Let's take a look at the three lines of code that you added. We want you to have a firm understanding of what the VBScript code is doing and how it is implemented within the HTML document. The first line defines a script. |
|
| The FOR argument specifies that this script is for the button named cmdClickMe, the name we have given our command button with the HTML <INPUT> tag. |
|
| The EVENT argument says that this script should be run when the button is clicked. TheLANGUAGE argument states that this is a VBScript module. |
|
<SCRIPT FOR="cmdClickMe" EVENT="onClick" LANGUAGE="VBScript">
|
|
| The second line is the only line of VBScript in this HTML document. The MsgBox function simply displays a message dialog. You will see more of the MsgBox function later in this tutorial. The third line marks the end of our script. |
|
| In the previous part, we simply inserted the VBScript module right after the HTML tag that defined the command button. While this method is functional, it is not the preferred approach. |
|
| HTML by itself can be confusing to read with all of its tags and text. Adding VBScript into the middle all of this just makes it even more complicated. |
|
| A more organized alternative is to place all of your script together within the HTML document. The following steps introduce you to this approach. |
|
|
|
|
|
|
| Working With VBScript |
|
| Preferred Method to Include VBScript |
|
| Re-open the HTML document that you created in part 2, if necessary, and remove the lines that you added there: |
|
<SCRIPT FOR="cmdClickMe" EVENT="onClick" LANGUAGE="VBScript">
|
|
| MsgBox "A simple example of VBScript for You." & Vbcrlf & "You are Learning VBScript from eBIZ Education!" |
|
| MsgBox "A simple example of VBScript for You." & Vbcrlf & "You are Learning VBScript from eBIZ Education!" |
|
</SCRIPT>
|
|
| Modify the document adding the scripting lines as shown in the light shading below: |
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Working With VBScript </TITLE>
<SCRIPT LANGUAGE="VBScript">
<!-- Instruct non-IE browsers to skip over VBScript modules.
Sub cmdClickMe_OnClick
MsgBox "A simple example of VBScript for You." & Vbcrlf
"You are Learning VBScript from eBIZ Education!"
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H1>Your First VBScript Example</H1>
<P> By utilizing VBScript you can give your web pages actions.
Click on the button below to see what we mean. </P>
<FORM NAME="frmExercise1">
<INPUT TYPE="Button" NAME="cmdClickMe" VALUE="Click Me">
</FORM>
</BODY>
</HTML>
|
|
| Save the file and test the file by loading it into Internet Explorer. When you try out the Click Me button, the result is the same as the previous example. |
|
| How It Works |
|
| This second method starts with the same <SCRIPT> tag as the previous example. At the center of this script are three lines that provide the functionality for our page. |
|
| The first line defines a sub-procedure called cmdClickMe_OnClick. This will be executed any time that the control cmdClickMe is clicked. |
|
| This type of procedure is referred to as an event procedure. The event is the user clicking the button. |
|
| The procedure that we associate with this event is executed every time the button is clicked. |
|
| Sub cmdClickMe_OnClick |
|
| On the second line we find the MsgBox function again, while the third line marks an end to our subroutine. |
|
|
|
|
|
|
| VBScript Variable |
|
| Introduction to Variables |
|
| A variable is a named location in computer memory that you can use for storage of data during the execution of your scripts. You can use variables to: |
|
• Store input from the user gathered via your web page
• Save data returned from functions
• Hold results from calculations |
|
| Let's look at a simple VBScript example to clarify the use of variables. |
|
| Sub cmdVariables_OnClick |
|
| Dim Name |
|
| Name = InputBox("Enter your name: ") |
|
| MsgBox "The name you entered was " & Name |
|
| End Sub |
|
| The first line of this example defines a sub procedure associated with the click event of a command button named cmdVariables. |
|
| On the second line we declare a variable named Name. We are going to use this variable to store the name of the user when it is entered. The third line uses the InputBox function to first prompt for, and then return, the user's name. |
|
| You will see more of the InputBox function later in this tutorial. The name it returns is stored in the Name variable. |
|
| The fourth line uses the MsgBox function to display the user's name. Finally, the sub procedure completes on line five. |
|
| Exactly how, and where, variables are stored is not important. What you use them for, and how you use them is important. That is what we will be looking at next. |
|
|
|
|
|
|
| VBScript Variable |
|
| Declaring Variables |
|
| There are two methods for declaring variables in VBScript, explicitly and implicitly. You usually declare variables explicitly with the Dim statement: |
|
| Dim Name |
|
| This statement declares the variable Name. You can also declare multiple variables on one line as shown below, although it is preferable to declare each variable separately: |
|
| Dim Name, Address, City, State |
|
| Variables can be declared implicitly by simply using the variable name within your script. This practice is not recommended. It leads to code that is prone to errors and more difficult to debug. |
|
| You can force VBScript to require all variables to be explicitly declared by including the statement Option Explicit at the start of every script. Any variable that is not explicitly declared will then generate an error. |
|
| Variable Naming Rules |
|
| When naming variables the following rules apply: |
|
• They must begin with an alphabetic character
• They cannot contain embedded periods
• They must be unique within the same scope. There is more on scopes later in this lesson.
• They must be no longer than 255 characters |
|
|
|
|
|
|
| VBScript Variable |
|
| Variants and Subtypes |
|
| VBScript has a single data type called a variant. Variants have the ability to store different types of data. The types of data that a variant can store are referred to as subtypes. The table below describes the subtypes supported by VBScript . |
|
| Subtype | Description of Uses for Each Subtype |
| Byte | Integer numbers between 0 to 255 |
| Boolean | True and False |
| Currency | Monetary values |
| Date | Date and time |
| Double | Extremely large numbers with decimal points |
| Empty | The value that a variant holds before being used |
| Error | An error number |
| Integer | Large integers between -32,768 and 32,767 |
| Long | Extremely large integers (-2,147,483,648 and 2,147,483,647) |
| Object | Objects |
| Null | No valid data |
| Single | Large numbers with decimal points |
| String | Character strings |
|
|
| Assigning Values |
|
| You assign a value to a variable by using the following format: |
|
| Variable_name = value |
|
| The following examples demonstrate assigning values to variables: |
|
| Name = "Raju" |
|
| WorkedHour = 50 |
|
| Overtime = True |
|
|
|
|
|
|
| VBScript Variable |
|
| Scope of Variables |
|
| The scope of a variable dictates where it can be used in your script. A variable's scope is determined by where it is declared. If it is declared within a procedure, it is referred to as a procedure-level variable and can only be used within that procedure. |
|
| If it is declared outside of any procedure, it is a script-level variable and can be used throughout the script. |
|
| The example below demonstrates both script-level and procedure-level variables. |
|
<SCRIPT>
|
|
| Dim counter |
|
| Sub cmdButton_onClick |
|
| Dim temp |
|
| End Sub |
|
</SCRIPT>
|
|
| The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the cmdButton_onClick sub-procedure. |
|
|
|
|
|
|
| VBScript Variable |
|
| Constants and Arrays |
|
| Constants: VBScript does not provide support for constants, such as you find in other programming languages. You can work around this by assigning values to variables that you have defined as shown in the example below. Here, TAX_RATE is our constant. |
|
<SCRIPT>
Dim TAX_RATE
TAX_RATE = .06
Function CalculateTaxes
CalculateTaxes = CostOfGoods * TAX_RATE
End Function
</SCRIPT>
|
|
| Arrays |
|
| The VBScript language provides support for arrays. You declare an array using the Dim statement, just as you did with variables: |
|
| Dim States(28) |
|
| The statement above creates an array with 29 elements. Why 29? Because VBScript arrays are zero-based, meaning that the first array element is indexed 0 and the last is the number specified when declaring the array. |
|
| You assign values to the elements of an array just as you would a variable, but with an additional reference (the index) to the element in which it will be stored: |
|
| States(5) = "Orissa" |
|
| States(6) = "Punjab" |
|
| Arrays can have multiple dimensions-VBScript supports up to 60. Declaring a two dimensional array for storing 29 states and their capitals could be done as follows: |
|
| Dim StateInfo(28,1) |
|
| To store values into this array you would then reference both dimensions. |
|
| StateInfo(18,0) = "Bihar" |
|
| StateInfo(18,1) = "Kerala" |
|
| VBScript also provides support for arrays whose size may need to change as the script is executing. These arrays are referred to as dynamic arrays. A dynamic array is declared without specifying the number of elements it will contain: |
|
| Dim Customers() |
|
| The ReDim statement is then used to change the size of the array from within the script: |
|
| ReDim Customers(100) |
|
| There is no limit to the number of times an array can be re-dimensioned during the execution of a script. To preserve the contents of an array when you are re-dimensioning, use the Preserve keyword: |
|
| ReDim Preserve Customers(100) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 comments: