0% found this document useful (0 votes)
271 views

Vbscript

The document provides an overview of VBScript including adding VBScript code to HTML pages, VBScript basics like data types and variables, operators, conditional statements, looping, procedures, functions, and coding conventions. It discusses using the SCRIPT tag to include VBScript in HTML, VBScript only having one data type called a variant, declaring and assigning variables, arrays, constants, and operator precedence.

Uploaded by

umayuvarani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views

Vbscript

The document provides an overview of VBScript including adding VBScript code to HTML pages, VBScript basics like data types and variables, operators, conditional statements, looping, procedures, functions, and coding conventions. It discusses using the SCRIPT tag to include VBScript in HTML, VBScript only having one data type called a variant, declaring and assigning variables, arrays, constants, and operator precedence.

Uploaded by

umayuvarani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 91

Introduction to VBScript Adding VBScript Code to an HTML Page VB Script Basics - VBScript Data Types VBScript Variables - VBScript

pt Constants VBScript Operators mathematicalcomparison-logical Using Conditional Statements Looping Through Code
Gomathi 1

VBScript Procedures type casting variables - math functions date functions string functions other functions - VBScript Coding Conventions Dictionary Object in VBScript - Err Object
Gomathi 2

Why VBScript
Visual Basic is compiled Language whereas VBScript is interpreted language. VBScript is used to create Active Server pages(ASPs), to create administration scripts for windows 95/98 NT,to extend or enhance the functionality of the Microsoft products like word and Excel.

Gomathi

It can also be used as client side scripting language for Internet Explorer. Netscape does not support VBScript as a client side scripting language.

Gomathi

Adding VBScript Code to an HTML Page


You can use the SCRIPT element to add VBScript code to an HTML page. The <SCRIPT> Tag VBScript code is written within paired <SCRIPT> tags. For example, a procedure to test a delivery date might appear as follows:
Gomathi 5

<SCRIPT LANGUAGE="VBScript"> <!-Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function --> </SCRIPT>
Gomathi 6

Beginning and ending <SCRIPT> tags surround the code. The LANGUAGE attribute indicates the scripting language. You must specify the language because browsers can use other scripting languages.

Gomathi

Notice that the CanDeliver function is embedded in comment tags (<!-- and -->). This prevents browsers that don't understand the <SCRIPT> tag from displaying the code. Since the example is a general function it is not tied to any particular form control you can include it in the HEAD section of the page
Gomathi 8

<HTML> <HEAD> <TITLE>Place Your Order</TITLE> <SCRIPT LANGUAGE="VBScript"> <!-Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function --> </SCRIPT> </HEAD> <BODY>
Gomathi 9

You can use SCRIPT blocks anywhere in an HTML page. You can put them in both the BODY and HEAD sections. However, you will probably want to put all general-purpose scripting code in the HEAD section in order to keep all the code together. Keeping your code in the HEAD section ensures that all code is read and decoded before it is called from within the BODY section
Gomathi 10

One notable exception to this rule is that you may want to provide inline scripting code within forms to respond to the events of objects in your form. For example, you can embed scripting code to respond to a button click in a form:

Gomathi

11

<HTML> <HEAD> <TITLE>Test Button Events</TITLE> </HEAD> <BODY> <FORM NAME="Form1"> <INPUT TYPE="Button" NAME="Button1" VALUE="Click"> <SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript"> MsgBox "Button Pressed!" </SCRIPT> </FORM> </BODY> </HTML>
Gomathi 12

Most of your code will appear in either Sub or Function procedures and will be called only when specified by your code. However, you can write VBScript code outside procedures, but still within a SCRIPT block. This code is executed only once, when the HTML page loads. This allows you to initialize data or dynamically change the look of your Web page when it loads
Gomathi 13

VBScript Data types


VBScript has only one data type called a variant. A variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Variant is the only data type which functions return.

Gomathi

14

At its simplest, a Variant can contain either numeric or string information. A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context.. Similarly, if you're working with data that can only be string data, VBScript treats it as string data. Of course, you can always make numbers behave as strings by enclosing them in quotation marks (" ").
Gomathi 15

Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, you can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time.
Gomathi 16

Gomathi

17

You can use conversion functions to convert data from one subtype to another. In addition, the VarType function returns information about how your data is stored within a Variant

Gomathi

18

VBScript Variables
As with algebra, VBScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for VBScript variable names: Must begin with a letter Cannot contain a period (.) Cannot exceed 255 characters In VBScript, all variables are of type variant, that can store different types of data.
Gomathi 19

Declaring (Creating) VBScript Variables


Creating variables in VBScript is most often referred to as "declaring" variables. You can declare VBScript variables with the Dim, Public or the Private statement. Like this: Dim x Dim carname Now you have created two variables. The name of the variables are "x" and "carname".
Gomathi 20

You can also declare variables by using its name in a script. Like this: carname="Volvo"Now you have also created a variable. The name of the variable is "carname". However, this method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running.
Gomathi 21

If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement. This statement forces you to declare all your variables with the dim, public or private statement.
Gomathi 22

Option Explicit Dim carname carname=some value Assigning Values to Variables You assign a value to a variable like this: carname="Volvo" x=10 The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of "10".

Gomathi

23

Lifetime of Variables
How long a variable exists is its lifetime. When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared. If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
Gomathi 24

VBScript Array Variables


An array variable is used to store multiple values in a single variable. In the following example, an array containing 3 elements is declared: Dim names(2)The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this: names(0)= Gomathi" names(1)=swetha names(2)="Shreeya"
Gomathi 25

VBScript Constants
A constant is a meaningful name that takes the place of a number or string and never changes. Creating Constants We can create user-defined constants in VBscript using the Const Statement. Const MyString=Good day Const MyAge=99
Gomathi 26

VBScript operators
VBScript has full range of operators including arithmetic operators, comparision operators, concatenation operators and logical operators. Operator Precedence When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. We can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained.
Gomathi 27

When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. Arithmetic and logical operators are evaluated in the following order of precedence
Gomathi 28

Gomathi

29

When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. Likewise, when addition and subtraction occur together in an expression, each operation is evaluated in order of appearance from left to right.

Gomathi

30

The string concatenation (&) operator is not an arithmetic operator, but in precedence it does fall after all arithmetic operators and before all comparison operators. The Is operator is an object reference comparison operator. It does not compare objects or their values; it checks only to determine if two object references refer to the same object
Gomathi 31

VBScript Conditional statements


Conditional statements are used to perform different actions for different decisions. In VBScript we have four conditional statements: If statement - executes a set of code when a condition is true If...Then...Else statement - select one of two sets of lines to execute If...Then...ElseIf statement - select one of many sets of lines to execute Select Case statement - select one of many sets of lines to execute
Gomathi 32

If...Then...Else Use the If...Then...Else statement if you want to execute some code if a condition is true select one of two blocks of code to execute If you want to execute only one statement when a condition is true, you can write the code on one line: If i=10 Then alert("Hello")There is no ..Else.. in this syntax.
Gomathi 33

Gomathi

34

Gomathi

35

Select Case You can also use the "Select Case" statement if you want to select one of many blocks of code to execute

Gomathi

36

Gomathi

37

This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed

Gomathi

38

VBScript Looping
Looping Statements Looping statements are used to run the same block of code a specified number of times. In VBScript we have four looping statements: For...Next statement - runs code a specified number of times For Each...Next statement - runs code for each item in a collection or each element of an array

Gomathi

39

Do...Loop statement - loops while or until a condition is true While...Wend statement - Do not use it use the Do...Loop statement instead

Gomathi

40

For...Next Loop Use the For...Next statement to run a block of code a specified number of times. The For statement specifies the counter variable (i), and its start and end values. The Next statement increases the counter variable (i) by one.

Gomathi

41

Gomathi

42

The Step Keyword


With the Step keyword, you can increase or decrease the counter variable by the value you specify. In the example below, the counter variable (i) is INCREASED by two, each time the loop repeats. For i=2 To 10 Step 2 some code Next
Gomathi 43

To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value. In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats. For i=10 To 2 Step -2 some code Next
Gomathi 44

Exit a For...Next You can exit a For...Next statement with the Exit For keyword. For i=1 To 10 If i=5 Then Exit For some code Next

Gomathi

45

For Each...Next Loop


A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array. Example <html> <body> <script type="text/vbscript"> Dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x In cars document.write(x & "<br />") Next </script> </body> </html>

Gomathi

46

Do...Loop If you don't know how many repetitions you want, use a Do...Loop statement. The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes true. Repeat Code While a Condition is True You use the While keyword to check a condition in a Do...Loop statement. Do While i>10 some code Loop
Gomathi 47

If i equals 9, the code inside the loop above will never be executed. Do some code Loop While i>10 The code inside this loop will be executed at least one time, even if i is less than 10. Repeat Code Until a Condition Becomes True You d to check a condition in a Do...Loop statement.
Gomathi 48

Do Until i=10 some code Loop If i equals 10, the code inside the loop will never be executed. Do some code Loop Until i=10 The code inside this loop will be executed at least one time, even if i is equal to 10
Gomathi 49

Exit a Do...Loop You can exit a Do...Loop statement with the Exit Do keyword. Do Until i=10 i=i-1 If i<10 Then Exit Do Loop The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.
Gomathi 50

VBScript procedures
VBScript has two kinds procedures: Sub procedure Function procedure

Gomathi

51

VBScript Sub Procedures


A Sub procedure: is a series of statements, enclosed by the Sub and End Sub statements can perform actions, but does not return a value can take arguments without arguments, it must include an empty set of parentheses ()
Gomathi 52

Sub mysub() some statements End Sub or Sub mysub(argument1,argument2) some statements End Sub

Gomathi

53

Example (IE Only) Sub mysub() alert("Hello World") End Sub

Gomathi

54

VBScript Function Procedures


A Function procedure: is a series of statements, enclosed by the Function and End Function statements can perform actions and can return a value can take arguments that are passed to it by a calling procedure without arguments, must include an empty set of parentheses () returns a value by assigning a value to its name
Gomathi 55

Function myfunction() some statements myfunction=some value End Function or Function Myfunction(argument1,argument2) some statements myfunction=some value End Function
Gomathi 56

Example (IE Only) function myfunction() myfunction=Date() end function

Gomathi

57

How to Call a Procedure


There are different ways to call a procedure. You can call it from within another procedure, on an event, or call it within a script.

Gomathi

58

Example (IE Only) Call a procedure when the user clicks on a button: <body> <button onclick="myfunction()">Click me</button> </body>

Gomathi

59

Procedures can be used to get a variable value: carname=findname()Here you call a Function called "findname", the Function returns a value that will be stored in the variable "carname".

Gomathi

60

Function procedures can calculate the sum of two arguments: Example (IE Only) Function myfunction(a,b) myfunction=a+b End Function

document.write(myfunction(5,9))
Gomathi 61

The function "myfunction" will return the sum of argument "a" and argument "b". In this case 14. When you call a procedure you can use the Call statement, like this: Call MyProc(argument)Or, you can omit the Call statement, like this: MyProc argument
Gomathi 62

VBScript Functions
This page contains all the built-in VBScript functions. The page is divided into following sections: Date/Time functions Conversion functions Format functions Math functions Array functions String functions Other functions

Gomathi

63

Gomathi

64

Gomathi

65

Gomathi

66

Function
Format Functions

Description
Top

FormatCurrency FormatDateTime FormatNumber FormatPercent

Returns an expression formatted as a currency value Returns an expression formatted as a date or time Returns an expression formatted as a number Returns an expression formatted as a percentage

Gomathi

67

Gomathi

68

Gomathi

69

Gomathi

70

Gomathi

71

Gomathi

72

VBScript Coding Conventions


What Are Coding Conventions? Coding conventions are suggestions that may help you write code using Microsoft Visual Basic Scripting Edition. Coding conventions can include the following: Naming conventions for objects, variables, and procedures Commenting conventions Text formatting and indenting guidelines
Gomathi 73

The main reason for using a consistent set of coding conventions is to standardize the structure and coding style of a script or set of scripts so that you and others can easily read and understand the code. Using good coding conventions results in precise, readable, and unambiguous source code that is consistent with other language conventions and as intuitive as possible
Gomathi 74

Constant Naming Conventions


Earlier versions of VBScript had no mechanism for creating user-defined constants. Constants, if used, were implemented as variables and distinguished from other variables using all uppercase characters. Multiple words were separated using the underscore (_) character. For example: USER_LIST_MAX NEW_LINE
Gomathi 75

While this is still an acceptable way to indentify your constants, you may want to use an alternative naming scheme, now that you can create true constants using the Const statement. This convention uses a mixed-case format in which constant names have a "con" prefix. For example: conYourOwnConstant
Gomathi 76

Gomathi

77

Variable Scope
Variables should always be defined with the smallest scope possible. VBScript variables can have the following scope.

Gomathi

78

Gomathi

79

Descriptive Variable and Procedure Names


The body of a variable or procedure name should use mixed case and should be as complete as necessary to describe its purpose. In addition, procedure names should begin with a verb, such as InitNameArray or CloseDialog

Gomathi

80

For frequently used or long terms, standard abbreviations are recommended to help keep name length reasonable. In general, variable names greater than 32 characters can be difficult to read. When using abbreviations, make sure they are consistent throughout the entire script. For example, randomly switching between Cnt and Count within a script or set of scripts may lead to confusion.
Gomathi 81

Gomathi

82

Gomathi

83

Gomathi

84

Gomathi

85

Gomathi

86

Gomathi

87

Gomathi

88

Gomathi

89

Gomathi

90

Gomathi

91

You might also like