VBScript



VBScript

Extra Credit Class Notes

By: Muhammad Sohail Rauf

What is VBScript?

VBScript is a light version of the “Microsoft Visual Basic and it is inserted in HTML documents. The internet browser read the HTML and identifies the VBScript. The VBScript code can be executed immediately or at a later point. It was developed so that millions of Visual Basic developers can use their knowledge in the internet scripting.

VBScript started as a client-side scripting language. The biggest problem to this language is that it was not and is not supported by Netscape Navigator. On the other hand the scripting language JavaScript was supported by both major browsers, Netscape Navigator and Internet Explorer. For that reason JavaScript has become the de facto standard for client-side scripting.

Location of VBScript Code:

VBScript code can be placed in both the ‘head’ and ‘body’ sections of the HTML page. Functions are mostly placed in the ‘head’ section while the body of the webpage is mostly located in the ‘body’ section. The statement tells the browser that VBScript code has been started and the statement identifies end of VBScript code.

VBScript Syntax:

The purpose of these notes is to introduce the syntax of VBScript. The ease of this scripting language is an attracting force within itself. This force demands any person with some programming knowledge to come forward and explore this exciting language.

The following topics will be covered in these notes:

|Writing Text |Conditional Statements |

|Formatting Text |Looping Statements |

|Code in ‘Head’ section |Date, Month, Day, Time |

|Code in ‘Body’ section |Date Function Type |

|Variables |Random Number Generator |

|Arrays |Upper/Lower Case |

|Sub-procedures |Sub-string |

|Functions |Format Currency |

Writing Text:

The code for writing text in VBScript is very similar to that in ASP. The ASP keywords ‘response.write’ is replaced by the keywords ‘document.write’. In the code below the ‘document.write(“Hello from VBScript!)’ is used in the body to print this statement on the webpage.

|Code: |

| |

| |

| |

| Writing Text in VBScript! |

| |

| |

| |

|document.write("Hello from VBScript!") |

| |

| |

| |

Result:

Hello from VBScript!

Formatting Text:

All HTML tags can also be used in the VBScript code to format the text in different ways. In the following example ‘font’ tag is used to specify the color while the ‘’ are used to represent different sizes of the text.

|Code: |

| |

| |

| |

| |

|document.write(" Welcome from VBScript! ") |

|document.write(" Welcome from VBScript!") |

|document.write(" Welcome from VBScript!) |

| |

| |

|Result: |

|Welcome from VBScript! |

|Welcome from VBScript! |

|Welcome from VBScript! |

VBScript in ‘HEAD’ section:

The VBScript code in the head section is executed when it is called or when an event is triggered. Functions are mostly defined in the head section. In the following code an alert window is coded in the head section. When the code is executed an alert window comes up first and when the user clicks ‘OK’ then the code in the body section is shown.

|Code: |

| |

| |

| |

| |

|alert("Press OK to see the webpage content.") |

| |

| |

| |

| |

| |

|The script in the head section is executed when it is called or when an event is triggered. Functions are mostly defined in the |

|head section. |

| |

| |

| |

|Result: |

| |

|[pic] |

|The script in the head section is executed when it is called or when an event is triggered. Functions are mostly defined in the |

|head section. |

Code in ‘BODY’ section:

The code in the ‘body’ section is executed as the page loads. This is the script that generates the content of the webpage. In the following code the printing statement in body section is executed as the page loads.

|Code: |

| |

| |

| |

| |

|document.write(“Scripts in the body section |

|are executed when the page is loading") |

| |

| |

| |

|Result: |

| |

|Scripts in the body section are executed when the page is loading. |

Variables in VBScript:

A variable acts as a container for information. It can be referenced by its name to see its value or assign it a new value. In VBScript the variables are of type variant that can store different types of data. A variable name must begin with a letter, cannot exceed 255 characters and, cannot contain a period (.). A variable that is declared in a specific procedure is destroyed when the procedure exits. This type of variable is called “local variable”. A variable that is declared outside the procedures can be used by all procedures on the page and is destroyed when the page is closed. In the following example, the variable ‘name’ is used to store the name of a person and then that variable is used to print the name on the webpage. The keyword ‘dim’ is used for the variable declaration.

|Code: |

| |

| |

| |

|dim name |

|name=“Muhammad Sohail Rauf" |

|document.write("My name is: " & name) |

| |

| |

| |

|Result: |

| |

|My name is: Muhammad Sohail Rauf |

Arrays in VBScript:

Arrays are used to assign more than one value to a single variable. The declaration of an array variable uses parenthesis ( ) after the variable name. Data can be stored and retrieved from an element using the index of that element in the specific array. Example of one dimensional array declaration is: “dim name(2)” which has size 3. Example of a two dimensional array declaration is: “dim table(2, 3)” which has 3 rows and 4 columns.

In the following example an array named ‘count’ is defined of length ‘6’. Different values have been stored at different indexes of that array. At the end a printing statement is used inside a ‘for’ loop to print values of all indices of the array.

|Code: |

| |

| |

| |

| |

|dim count(5) |

|count(0)="First" |

|count(1)="Second" |

|count(2)="Third" |

|count(3)="Fourth" |

|count(4)="Fifth" |

|count(5)="Sixth" |

| |

|for i=0 to 5 |

|document.write(count(i) & "") |

|next |

| |

| |

| |

|Result: |

| |

|First |

|Second |

|Third |

|Fourth |

|Fifth |

|Sixth |

Sub Procedure:

A sub-procedure is a group of statements enclosed within ‘Sub’ and ‘End Sub’ statements. It can take arguments that are passed by its calling procedure but it does not return a value. We can call a sub-procedure by the statement “Call name (argument)”.

In the following example a sub-procedure named ‘mySub()’ is defined in the head section of the html code. The purpose of this sub-procedure is to show a message box containing a text message. The sub-procedure is called from the body section. When the page loads a message box appears first and when the user clicks ‘OK’ button the content of the body section are printed on the webpage.

|Code: |

| |

| |

| |

| |

|Sub mySub() |

|msgbox("This is a sub-procedure") |

|End Sub |

| |

| |

| |

| |

| |

|call mySub() |

| |

| A sub procedure does not return a value. |

| |

| |

| |

|Result: |

| |

|[pic] |

| |

|A sub procedure does not return a value. |

Function:

A function is a series of statements enclosed within ‘Function’ and ‘End Function’ statements. It can take arguments passed to it by its calling procedure and can also return a value. The function can be called by the statement ‘name( )’. Functions are mostly defined in the head section of the html code.

In the following example, a function named ‘myname() is defined in the head section. This function stores a name in the ‘myname’ variable. When the function is called in the body section, it passes the stored name to the calling procedure. That name is then printed by the printing statement present in the body section.

|Code: |

| |

| |

| |

| |

|Function myName() |

|myName = "Muhammad Sohail Rauf" |

|End Function |

| |

| |

| |

| |

| |

|document.write("My name is " & myName()) |

| |

| A function procedure can return a value. |

| |

| |

|Result: |

| |

|My name is Muhammad Sohail Rauf |

|A function procedure can return a value. |

Conditional Statements:

Conditional statements are used when the programmer wants to execute different set of statements in different situations. The three most commonly used conditional statements are:

• IF-THEN-ELSE

• IF-THEN-ELSEIF

• CASE

IF-THEN-ELSE:

This conditional statement is used when we want to execute one of two codes. If the condition is true the code in the ‘IF’ block is executed. On the other hand if the condition is false the code in the ‘ELSE’ block is executed.

In the following example, a variable ‘i’ is assigned the value ’10’ and then an ‘IF-THEN-ELSE’ statement is used to print different statements in the cases that the variable is greater than or less that eight. The code in the ‘IF’ block will execute if the value of the variable is greater than 8 (which it is), whereas the code in the ‘ELSE’ block will be executed if the value is less than 8.

|Code: |

| |

| |

| |

| |

| |

|Dim i |

|i = 10 |

|if i > 8 then |

|document.write("More than Eight.") |

|else |

|document.write("Less than Eight.") |

|end if |

| |

| |

| |

| |

|Result: |

| |

|More than Eight. |

IF-THEN-ELSEIF:

This statement is used when we need to execute different codes in different situations and the situations are more than two. In the example below a variable is assigned the value ‘10’. After that an ‘IF-THEN-ELSEIF’ statement is used to print different statements for different values of the variable i. The condition in the first ‘ELSEIF’ block is true so the value “Ten” will be printed as the webpage loads.

|Code: |

| |

| |

| |

| |

|Dim i |

|i = 10 |

| |

|If i = 9 then |

|document.write("Nine") |

|elseif i = 10 then |

|document.write("Ten") |

|elseif i = 11 then |

|document.write("Eleven") |

|else |

|document.write(“Other") |

|end if |

| |

| |

| |

|Result: |

| |

|Ten |

CASE STATEMENT:

This statement is similar to the ‘IF-THEN-ELSEIF’ statement. It is used when we have more than two possibilities. Different pieces of code are executed for different conditions. In the example below a variable is assigned the vale ‘5’ and then different statements are set to be executed for different values of that variable through a ‘CASE’ statement. As the value is not one of the provided cases of the statement the code in the ‘OTHER’ clause is executed resulting the printing of text ‘Other’ on the webpage.

|Code: |

| |

| |

| |

| |

|Dim i |

|i = 5 |

| |

|select case i |

|case 9 |

|document.write("Nine") |

|case 10 |

|document.write("Ten") |

|case 11 |

|document.write("Eleven") |

|case else |

|document.write("Other") |

|end select |

| |

| |

| This is a very simple example of using CASE |

|statement in VBScript. |

| |

| |

| |

|Result: |

| |

|Other |

|This is a very simple example of using CASE statement in VBScript. |

Looping in VBScript:

In any programming language loops are used when we need to execute a block of code more than one times. The most frequent loops that are used in VBScript are the following:

• FOR-NEXT

• FOR-EACH-NEXT

• DO-LOOP

FOR-NEXT LOOP:

This loop is used when the programmer has to execute a block of code a specified number of times. A counter variable is used to start and stop the loop. The “step” clause is used to increment the counter by a specified value. For example, in the statement ‘For i=2 To 10 Step 2’, the counter i is incremented by 2 each time through the loop.

In the example below the loop is executed nine times and prints a text statement including the value of counter on the webpage after each iteration of the loop.

|Code: |

| |

| |

| |

| |

|for i = 0 to 8 |

|document.write("The counter is: " & i & "") |

|next |

| |

| |

| |

|Result: |

| |

|The counter is: 0 |

|The counter is: 1 |

|The counter is: 2 |

|The counter is: 3 |

|The counter is: 4 |

|The counter is: 5 |

|The counter is: 6 |

|The counter is: 7 |

FOR-EACH-NEXT LOOP:

This loop executes a block of code for each item in a collection or an array. It does not require specifying the number of times the loop will execute. In the code segment below an array named ‘games’ is defined and then values are assigned to different indices of that array. After that a ‘FOR-NEXT’ loop is used to print values at all indices of the array. We did not specify how many times the loop will execute as it is designed to execute for all elements of the array.

|Code: |

| |

| |

| |

| |

|dim games(2) |

|games(0) = "Hockey" |

|games(1) = "Soccor" |

|games(2) = "Football" |

| |

|for each x in games |

|document.write(x & "") |

|next |

| |

| |

| |

|Result: |

| |

|Hockey |

|Soccor |

|Football |

DO-LOOP:

This loop is executed while a condition is true or until a condition becomes true. In the example below a variable ‘i’ is assigned the value ‘0’. After that a ‘DO WHILE’ loop is used to print the value of counter and incrementing the counter each time through the loop. The loop stops when the value of ‘i’ becomes 10.

|Code: |

| |

| |

| |

| |

|i=0 |

|do while i < 10 |

|document.write(i & "") |

|i=i+1 |

|loop |

| |

| |

| |

|Result: |

| |

|0 |

|1 |

|2 |

|3 |

|4 |

|5 |

|6 |

|7 |

|8 |

|9 |

Date, Time, Day, and Month:

The following code segments prints the current date, time, day of the week and the month. Here the ‘date()’ function gets the date and the ‘time()’ function gets the time. The next two operations used the date function to get the weekday name and the month name.

|Code: |

| |

| |

| |

| |

|document.write("Today's date is" & date()) |

|document.write("") |

| |

|document.write("The time is " & time()) |

|document.write("") |

| |

|document.write("The current day is: ") |

|document.write(WeekdayName(weekday(date))) |

|document.write("") |

| |

|document.write("The current month is: ") |

|document.write(MonthName(month(date))) |

| |

| |

| |

| |

|Result: |

| |

|Today's date is12/1/2003 |

| |

|The time is 3:26:42 AM |

| |

|The current day is: Monday |

| |

|The current month is: December |

Date Formats:

The following code segment displays the different formats of the date used in VBScript. These formats are the following respectively:

• General Date

• Long Date

• Short Date

• Long Time

• Short Time

Anyone of these date formats can be used according to the need of the programmer.

|Code: |

| |

| |

| |

| |

|document.write(FormatDateTime(date(),vbgeneraldate)) |

|document.write("") |

|document.write(FormatDateTime(date(),vblongdate)) |

|document.write("") |

|document.write(FormatDateTime(date(),vbshortdate)) |

|document.write("") |

|document.write(FormatDateTime(now(),vblongtime)) |

|document.write("") |

|document.write(FormatDateTime(now(),vbshorttime)) |

| |

| |

| |

|Result: |

| |

|12/1/2003 |

|Monday, December 01, 2003 |

|12/1/2003 |

|3:28:23 AM |

|03:28 |

Uppercase and Lowercase:

The code segment below displays a text statement in uppercase and lowercase formats. Here we make a variable and store some text in first. Then the functions ‘ucase’ is used to print the text in uppercase while the function ‘lcase’ is used to print it in the lowercase.

|Code: |

| |

| |

| |

| |

|txt="Computer & Information Sciences." |

|document.write(ucase(txt)) |

|document.write("") |

|document.write(lcase(txt)) |

| |

| |

| |

|Result: |

| |

|COMPUTER & INFORMATION SCIENCES. |

|computer & information sciences. |

Generating Random Numbers:

The following code segment generates and displays the random numbers on the webpage. This piece of code is using three of VBScript’s built-in functions. Here the function ‘Int’ makes the code generate only integers, ‘rnd()’ is used to generate the random numbers.

The function ‘randomize()’ is used to generate random numbers whenever the page is opened. If this function is not used whenever the webpage will be opened, it will always generate the same sequence of random numbers.

|Code: |

| |

| |

| |

| |

| |

|randomize() |

|randomNumber=Int(100 * rnd()) |

|document.write("A random number: " & randomNumber & "") |

| |

| |

| |

| |

|Result: |

| |

|A random number: 82 |

Sub-String:

This method is used to get some part of a string. The code below first makes a text variable and then extracts a piece of the variable string through the ‘Mid’ function. This function needs three arguments, which are the text string, starting point, and length from the starting point that is needed to be extracted respectively.

Here the 12th position in the string is the ‘C’ of ‘COMS-463’. The code will start from this ‘C’ and go until 8 letters to extract the required string.

|Code: |

| |

| |

| |

| |

|sometext="Welcome to COMS-463 at MSU!!" |

|document.write(Mid(sometext, 12, 8)) |

| |

| |

| |

|Result: |

| |

|COMS-463 |

Format-Currency:

The code segment formats and prints the currency value when the page loads. Here ‘FormatCurrency’ is the method used to do that and it take two parameters that are the value and the decimal places respectively. The function round the value to the given decimal place and also puts a dollar sign before it.

|Code: |

| |

| |

| |

| |

|document.write(FormatCurrency(20000.578,2)) |

| |

| |

| |

|Result: |

| |

|$20,000.58 |

Sample Questions:

Short Answers:

Q-1: Write the VBScript code to make a two dimensional array variable named ‘table’ with 3 rows and 4 columns.

Ans:

dim table(2,3)

-----------------------------------------------------------

Q-2: Write VBScript code to print current date on the webpage.

Ans:

Document.write(Current date is: &date())

------------------------------------------------------------

Q-3: Write the VBScript code to print the statement “Computer & Information Science” in uppercase and lowercase.

Ans:

Uppercase

document.write(ucase(“Computer & Information Science”)

Lowercase

document.write(lcase(“Computer & Information Science”)

------------------------------------------------------------------

Q-4: What does the ‘Int’ function do in the code to generate the random numbers?

Ans:

It makes the ‘rnd() function generate only integers. If it is not used the ‘rnd()’ function will generate all fractional number.

---------------------------------------------------------------

Q-5: List the arguments taken by the sub-string ‘Mid’ method respectively.

Ans:

• String

• Starting Position

• Length

True/False:

Q-1: Functions are mostly located in the body section.

Ans: False

------------------------------------------------------------

Q-2: VBScript is supported by both ‘Internet Explorer’ and ‘Netscape Navigator’.

Ans: False

-----------------------------------------------------------

Q-3: HTML tags can be used in the VBScript code.

Ans: True

-----------------------------------------------------------

Q-4: For-Next loop is used to loop through each element of an array location or a container.

Ans: False

-----------------------------------------------------------

Q-5: In VBScript sub-procedures do not return a value where the functions can return value to the calling procedure.

Ans: True

Multiple Choices:

Q-1: The function that is used to extract a sub-string is:

a. rnd()

b. Int

c. Mid (Correct)

d. Sub

---------------------------------------------------------

Q-2: The array declaration ‘dim table(2,3) has the following number of rows and columns respectively.

a. 2 columns and 3 rows

b. 3 columns and 4 rows

c. 2 rows and 3 columns

d. 3 rows and 4 columns (Correct)

---------------------------------------------------------

Q-3: One of the following is not used as a parameter in the sub-string function of the VBScript.

a. Length of required sub-string.

b. String

c. Starting position.

d. All of them are required.

---------------------------------------------------------

Q-4: One of the following is not one of the rules in defined the names of variables

a. Variable name cannot contain a period (.).

b. Variable name cannot exceed 128 characters.

c. Variable name must begin with a letter.

d. Variable name cannot exceed 255 characters.

-----------------------------------------------------------

Q-5: Which one of the following date function pairs is equivalent?

a. vbshortdate and vblongdate

b. vbshortdate and vbshorttime

c. vblongdate and vbgeneraldate

d. vbshortdate and vbgeneraldate (Correct).

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download