100% found this document useful (1 vote)
218 views

What Is VBScript?

VBScript is a lightweight scripting language that is used to add interactivity to web pages. VBScript code can be added between <script> tags in the <head> or <body> sections of an HTML document. VBScript variables do not need to be declared with a specific data type and follow rules for naming like starting with a letter. VBScript includes different types of procedures like Sub and Function procedures to perform actions or return values. It also supports conditional statements, loops, and functions to manipulate strings and perform mathematical operations on numbers.

Uploaded by

soundarpandiyan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
218 views

What Is VBScript?

VBScript is a lightweight scripting language that is used to add interactivity to web pages. VBScript code can be added between <script> tags in the <head> or <body> sections of an HTML document. VBScript variables do not need to be declared with a specific data type and follow rules for naming like starting with a letter. VBScript includes different types of procedures like Sub and Function procedures to perform actions or return values. It also supports conditional statements, loops, and functions to manipulate strings and perform mathematical operations on numbers.

Uploaded by

soundarpandiyan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

VB Script

What is VBScript?

 VBScript is a scripting language


 A scripting language is a lightweight programming language
 VBScript is a light version of Microsoft's programming language Visual Basic

How to add VBScript?


<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>

Where to Put the VBScript?


<html>
<head>
<script type="text/vbscript">
....
</script>
</head>
<body>
<script type="text/vbscript">
....
</script>
</body>

VBScript Variables

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.

Declaring (Creating) VBScript Variables


Dim x;
Dim carname;

Dim names(2)  array

Dim table(4,6)  Two Dimensional Array

Assigning Values to Variables


carname="Volvo"
x=10

Dim names(2)  names(0)="Tove"


names(1)="Jani"
names(2)="Stale"

VBScript Procedures

In VBScript, there are two kinds of procedures:

 Sub procedure
 Function procedure

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 ()

Sub mysub(argument1,argument2)
  some statements
End Sub

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

Function myfunction(argument1,argument2)
  some statements
  myfunction=some value
End Function

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

If...Then...Else If...Then...ElseIf

<html> <html>
<body> <body>
<script type="text/vbscript"> <script type="text/vbscript">
Function greeting() Function greeting()
i=hour(time) i=hour(time)
If i < 10 Then If i = 10 Then
  document.write("Good morning!")   document.write("Just started...!")
Else ElseIf i = 11 then
  document.write("Have a nice day!")   document.write("Hungry!")
End If ElseIf i = 12 then
End Function   document.write("Ah, lunch-time!")
</script> ElseIf i = 16 then
</head>   document.write("Time to go home!")
Else
<body onload="greeting()">   document.write("Unknown")
</body> End If
End Function
</html> </script>
</head>
<body onload="greeting()">
</body>

</html>
Select Case

<html>
<body>
<script type="text/vbscript">
d=weekday(date)
Select Case d
  Case 1
    document.write("Sleepy Sunday")
  Case 2
    document.write("Monday again!")
  Case 3
    document.write("Just Tuesday!")
  Case 4
    document.write("Wednesday!")
  Case 5
    document.write("Thursday...")
  Case 6
    document.write("Finally Friday!")
  Case else
    document.write("Super Saturday!!!!")
End Select
</script>

</body>
</html>

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
 Do...Loop statement - loops while or until a condition is true
 While...Wend statement - Do not use it - use the Do...Loop statement instead

For i=2 To 10 Step 2 For i=10 To 2 Step -2


  some code   some code
Next Next
For Each...Next Loop Do...Loop

<html>
<body> Do While i>10
  some code
<script type="text/vbscript"> Loop
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab" Do
cars(2)="BMW"   some code
Loop While i>10
For Each x In cars
  document.write(x & "<br />")
Next Do Until i=10
</script>   some code
Loop
</body>
</html>

Functions VBScript vs JavaScript

VBScript Javascript
Function Syntax Function Syntax
InStr Dim txt,pos
txt="This is a beautiful day!"
var str="Hello world!";
pos=InStr(txt,"his")
document.write(str.indexOf("Hello") + "<br />");
document.write(pos)
indexOf() document.write(str.indexOf("World") + "<br
/>");
Output:
document.write(str.indexOf("world"));
2
InStrRev Dim txt,pos
txt="This is a beautiful day!"
pos=InStrRev(txt,"his")
document.write(pos)

Output:

2
LCase Dim txt toLowerC var str="Hello World!";
txt="THIS IS A BEAUTIFUL DAY!" ase() document.write(str.toLowerCase());
document.write(LCase(txt))

Output:

this is a beautiful day!


Left Dim txt
txt="This is a beautiful day!"
document.write(Left(txt,11))

Output:

This is a b
Len Dim txt
txt="This is a beautiful day!"
document.write(Len(txt))

Output:

24
LTrim Dim txt
txt=" This is a beautiful day! "
document.write(LTrim(txt))

Output:

"This is a beautiful day! "


RTrim Dim txt
txt=" This is a beautiful day! "
document.write(RTrim(txt))

Output:

" This is a beautiful day!"


Trim Dim txt
txt=" This is a beautiful day! "
document.write(Trim(txt))

Output:

"This is a beautiful day!"


Mid Dim txt
txt="This is a beautiful day!"
document.write(Mid(txt,1,11))

Output:

This is a b
Replace Dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beau
tiful","horrible"))
replace()
Output:

This is a horrible day!


Right

StrComp

StrReverse

UCase toUpperC var str="Hello world!";


ase() document.write(str.toUpperCase());

Math Functions

VBScript JavaScript
Function Example Function Example
Abs document.write(Abs(48.4) & "<br />") abs(x) document.write(Math.abs(7.25) + "<br />");
document.write(Abs(-48.4)) document.write(Math.abs(-7.25) + "<br />");
document.write(Math.abs(7.25-10));
Output:
7.25
48.4 7.25
48.4 2.75
Cos document.write(Cos(50.0)) cos(x) document.write(Math.cos(3) + "<br />");
document.write(Math.cos(-3) + "<br />");
Output: document.write(Math.cos(0) + "<br />");
document.write(Math.cos(Math.PI) + "<br />");
0.964966028492113 document.write(Math.cos(2*Math.PI));
Log document.write(Log(38.256783227)) log(x)

Output:

3.64432088381777
Rnd document.write(Rnd) random() document.write(Math.random());

Output:

0.7055475
Sin document.write(Sin(47)) sin(x) document.write(Math.sin(3) + "<br />");
document.write(Math.sin(-3) + "<br />");
Output: document.write(Math.sin(0) + "<br />");
document.write(Math.sin(Math.PI) + "<br />");
0.123573122745224 document.write(Math.sin(Math.PI/2));
Sqr document.write(Sqr(9)) sqrt(x) document.write(Math.sqrt(0) + "<br />");
document.write(Math.sqrt(1) + "<br />");
Output: document.write(Math.sqrt(9) + "<br />");
document.write(Math.sqrt(0.64) + "<br />");
3 document.write(Math.sqrt(-9));
Tan document.write(Tan(40)) tan(x)

Output:

-1.1172149309239
round(x) document.write(Math.round(0.60) + "<br />");
document.write(Math.round(0.50) + "<br />");
document.write(Math.round(0.49) + "<br />");
document.write(Math.round(-4.40) + "<br />");
document.write(Math.round(-4.60));

1
1
0
-4
-5
pow(x,y) document.write(Math.pow(0,0) + "<br />");
document.write(Math.pow(0,1) + "<br />");
document.write(Math.pow(1,1) + "<br />");
document.write(Math.pow(1,10) + "<br />");
document.write(Math.pow(2,3) + "<br />");
document.write(Math.pow(-2,3) + "<br />");
document.write(Math.pow(2,4) + "<br />");
document.write(Math.pow(-2,4) + "<br />");

1
0
1
1
8
-8
16
16

Date/Time Functions

Function Description
CDate Converts a valid date and time expression to the variant of
subtype Date
Date Returns the current system date
DateAdd Returns a date to which a specified time interval has been added
DateDiff Returns the number of intervals between two dates
DatePart Returns the specified part of a given date
DateSerial Returns the date for a specified year, month, and day
DateValue Returns a date
Day Returns a number that represents the day of the month (between
1 and 31, inclusive)
FormatDateTime Returns an expression formatted as a date or time
Hour Returns a number that represents the hour of the day (between 0
and 23, inclusive)
IsDate Returns a Boolean value that indicates if the evaluated expression
can be converted to a date
Minute Returns a number that represents the minute of the hour (between
0 and 59, inclusive)
Month Returns a number that represents the month of the year (between
1 and 12, inclusive)
MonthName Returns the name of a specified month
Now Returns the current system date and time
Second Returns a number that represents the second of the minute
(between 0 and 59, inclusive)
Time Returns the current system time
Timer Returns the number of seconds since 12:00 AM
TimeSerial Returns the time for a specific hour, minute, and second
TimeValue Returns a time
Weekday Returns a number that represents the day of the week (between 1
and 7, inclusive)
WeekdayName Returns the weekday name of a specified day of the week
Year Returns a number that represents the year

You might also like