VBScript
v VBScript is a
Microsoft scripting language.
v VBScript is
the default scripting language in ASP.
v Client-side
VBScript only works in Internet Explorer!!!
<html>
<body>
<script type="text/vbscript">
document.write("This is my first VBScript!")
</script>
</body>
</html>
<body>
<script type="text/vbscript">
document.write("This is my first VBScript!")
</script>
</body>
</html>
<html>
<body>
<script
type="text/vbscript">
document.write("<font
color=green size=7>Hello World!</font>")
</script>
</body>
</html>
<html>
<head>
<script
type="text/vbscript">
function
choksi()
alert("Hello
World!")
end
function
</script>
</head>
<body
onload="choksi()">
<p>We
usually use the head section for functions (to be sure that the functions are
loaded before they are called).</p>
</body>
</html>
<html>
<head>
<script
type="text/vbscript">
function
choksi()
document.write("Hello
World!")
end
function
</script>
</head>
<body>
<input
type="button" value="click here"
onclick="choksi()">
<p>We
usually use the head section for functions (to be sure that the functions are
loaded before they are called).</p>
</body>
</html>
<body>
<script
type="text/vbscript">
Dim
firstname
Dim
lastname
firstname="ahmad"
lastname="choksi"
document.write(firstname)
document.write("<br>")
document.write(lastname)
</script>
<p>The
script above declares a variable, assigns a value to it, and displays the
value. Then, it changes the value, and displays the value again.</p>
</body>
</html>
<body>
<script
type="text/vbscript">
Dim
name,surname
name="ahmad"
surname="choksi"
document.write("My
name is: " & name &"<br>")
document.write("My
surname is: " & surname)
</script>
</body>
</html>
<html>
<body>
<script
type="text/vbscript">
Dim
famname(5)
famname(0)="Jan
Egil"
famname(1)="Tove"
famname(2)="Hege"
famname(3)="Stale"
famname(4)="Kai
Jim"
famname(5)="Borge"
document.write(famname(5))
</script>
</body>
</html>
<html>
<body>
<script
type="text/vbscript">
Dim
famname(5)
famname(0)="Jan
Egil"
famname(1)="Tove"
famname(2)="Hege"
famname(3)="Stale"
famname(4)="Kai
Jim"
famname(5)="Borge"
For
i=0 To 5
document.write(famname(i)
& "<br />")
Next
</script>
</body>
</html>
<head>
<script
type="text/vbscript">
Sub
mySub()
msgbox("This is a Sub procedure")
End
Sub
</script>
</head>
<body>
<script
type="text/vbscript">
Call
mySub()
</script>
<p>A
Sub procedure does not return a result.</p>
</body>
</html>
<html>
<head>
<script
type="text/vbscript">
Function
myFunction()
myFunction = "BLUE"
End
Function
</script>
</head>
<body>
<script
type="text/vbscript">
document.write("My
favorite color is " & myFunction())
</script>
<p>A
Function procedure can return a result.</p>
</body>
</html>
Program-11 Use of Arithmetic or Math
Operators.
<html>
<body>
<script
type="text/vbscript">
dim
a,b
a=50
b=15
document.write("Sum
of a and b is="&(a+b)&"<br>")
document.write("Multiplication
of a and b is="&(a*b)&"<br>")
document.write("Subtraction
of a and b is="&(a-b)&"<br>")
document.write("Division
of a and b is="&(a/b)&"<br>")
document.write("Modulus
of a and b is="&(a mod b)&"<br>")
</script>
</body>
</html>
Program-12 Use of Comparison Operators.
<html>
<body>
<script
type="text/vbscript">
dim
a,b
a=50
b=15
document.write("a
equal to b="&(a=b)&"<br>")
document.write("a
greater than b="&(a>b)&"<br>")
document.write("a
less than b="&(a<b)&"<br>")
document.write("a
greater than or equal to b="&(a>=b)&"<br>")
document.write("a
less than or equal to b="&(a<=b)&"<br>")
document.write("a
not equal to b="&(a<>b)&"<br>")
</script>
</body>
</html>
Program-13 Use of Logical Operators.
<html>
<body>
<script
type="text/vbscript">
dim
a,b
a=50
b=15
dim
x,y,z
if
a>10 and b<20 then
x=true
else
x=false
end if
document.write("And
operator="&(x)&"<br>")
if
a<40 or b>10 then
y=true
else
y=false
end if
document.write("Or
operator="&(y)&"<br>")
if not
a=not y then
z=true
else
z=false
end if
document.write("NOT
operator ="&(z)&"<br>")
</script>
</body>
</html>
Program-14 If statement.
<head>
<script
type="text/vbscript">
dim i
i=20
If i > 18 Then
document.write("You are eligible for
voting")
End If
</script>
</head>
<html>
<head>
<script
type="text/vbscript">
dim i
i=20
If i = 20 Then
i=i+5
document.write("new
value of i is="&i)
End If
</script>
</head>
</html>
Program-16 If...Then..Else statement.
<head>
<script
type="text/vbscript">
dim i
i=20
If i > 18 Then
document.write("You
are eligible for voting")
Else
document.write("You
are not eligible for voting")
End If
</script>
</head>
<html>
<head>
<script
type="text/vbscript">
dim i
i=20
If i > 18 Then
i=i+5
document.write("new
value of a="&i)
Else
i=i-5
document.write("new
value of a="&i)
End If
</script>
</head>
</html>
Program-18 If...Then..Elseif statement.
<html>
<body>
<script
type="text/vbscript">
dim
percentage
dim
result
percentage=65
if
percentage >70 then
result="distiction"
elseif
percentage>=60 and percentage<70 then
result="first
class"
elseif
percentage>=50 and percentage<60 then
result="second
class"
elseif
percentage>=40 and percentage<50 then
result="third
class"
else
result="pass
class"
end if
document.write("Result
is="&result)
</script>
</body>
</html>
Program-19 Select Case statement.
<html>
<body>
<script
type="text/vbscript">
d=4
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>
Program-20 For...Next Loop.
Ø Use the For...Next statement to run a block of
code a specified number of times.
<html>
<body>
<script
type="text/vbscript">
For i
= 0 To 5
document.write("The
number is " & i & "<br />")
Next
</script>
</body>
</html>
Program-21 The Step Keyword.
Ø With the Step keyword, you can increase or decrease the
counter variable by the value you specify
<html>
<body>
<script type="text/vbscript">
For i = 0 To 10 step 2
document.write("The number is " & i & "<br />")
Next
</script>
</body>
</html>
<body>
<script type="text/vbscript">
For i = 0 To 10 step 2
document.write("The number is " & i & "<br />")
Next
</script>
</body>
</html>
Program-22 The Step Keyword.
<html>
<body>
<script
type="text/vbscript">
For i
= 10 To 0 step -2
document.write("The
number is " & i & "<br />")
Next
</script>
</body>
</html>
Program-23 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.
<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>
Program-24 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.
<html>
<body>
<script
type="text/vbscript">
i=0
Do
While i < 10
document.write(i
& "<br />")
i=i+1
Loop
</script>
</body>
</html>
Program-25 Do...Loop.
<html>
<body>
<script
type="text/vbscript">
i=0
Do
document.write(i
& "<br />")
i=i+1
Loop
While i < 10
</script>
</body>
</html>
Program-26 Do...Loop.(Until keyword)
<html>
<body>
<script
type="text/vbscript">
i=0
Do Until i>=
10
document.write(i & "<br
/>")
i=i+1
Loop
</script>
</body>
</html>
Program-27 Do...Loop. (Until
keyword)
<html>
<body>
<script
type="text/vbscript">
i=0
Do
document.write(i
& "<br />")
i=i+1
Loop
Until i >= 10
</script>
</body>
</html>
<html>
<body>
<script
type="text/vbscript">
document.write("Today's
date is " & Date()&"<br>")
document.write("The
time is " & Time())
</script>
</body>
</html>
<html>
<body>
<p>VBScripts'
function <b>WeekdayName</b> is used to get a weekday:</p>
<script
type="text/vbscript">
document.write("<p>")
document.write(WeekDayName(1))
document.write("<br
/>")
document.write(WeekDayName(2))
document.write("</p><p>")
document.write("Get
the abbreviated name of a weekday:")
document.write("<br
/>")
document.write(WeekDayName(1,True))
document.write("<br
/>")
document.write(WeekDayName(2,True))
document.write("</p><p>")
document.write("Get
the current weekday:")
document.write("<br
/>")
document.write(WeekdayName(weekday(Date)))
document.write("<br
/>")
document.write(WeekdayName(weekday(Date),
True))
document.write("</p>")
</script>
</body>
</html>
<body>
<p>VBScripts'
function <b>MonthName</b> is used to get a month:</p>
<script
type="text/vbscript">
document.write("<p>")
document.write(MonthName(1))
document.write("<br
/>")
document.write(MonthName(2))
document.write("</p><p>")
document.write("Here
is how you get the abbreviated name of a month:")
document.write("<br
/>")
document.write(MonthName(1,True))
document.write("<br
/>")
document.write(MonthName(2,True))
document.write("</p><p>")
document.write("Here
is how you get the current month:")
document.write("<br
/>")
document.write(MonthName(Month(Date)))
document.write("<br
/>")
document.write(MonthName(Month(Date),True))
document.write("</p>")
</script>
</body>
</html>
<html>
<body>
<script
type="text/vbscript">
document.write("Today's
day is " & WeekdayName(Weekday(Date)))
document.write("<br
/>")
document.write("The
month is " & MonthName(Month(Date)))
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
document.write(FormatDateTime(Date(),vbGeneralDate))
document.write("<br
/>")
document.write(FormatDateTime(Date(),vbLongDate))
document.write("<br
/>")
document.write(FormatDateTime(Date(),vbShortDate))
document.write("<br
/>")
document.write(FormatDateTime(Now(),vbLongTime))
document.write("<br
/>")
document.write(FormatDateTime(Now(),vbShortTime))
</script>
<p>Syntax
for FormatDateTime: FormatDateTime(Date,namedformat).</p>
</body>
</html>
Program-33 Is this a date?
<html>
<body>
<script
type="text/vbscript">
somedate="10/30/99"
document.write(IsDate(somedate))
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
txt="Have
a nice day!"
document.write(UCase(txt))
document.write("<br
/>")
document.write(LCase(txt))
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
fname="
Bill "
document.write("Hello"
& Trim(fname) & "Gates<br />")
document.write("Hello"
& RTrim(fname) & "Gates<br />")
document.write("Hello"
& LTrim(fname) & "Gates<br />")
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
sometext
= "Hello Everyone!"
document.write(StrReverse(sometext))
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
i =
48.66776677
j =
48.3333333
document.write(Round(i))
document.write("<br
/>")
document.write(Round(j))
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
sometext="Welcome
to our Web Site!!"
document.write(Left(sometext,5))
document.write("<br
/>")
document.write(Right(sometext,5))
</script>
</body>
</html>
<body>
<script
type="text/vbscript">
sometext="Welcome
to this Web!!"
document.write(Replace(sometext,
"Web", "Page"))
</script>
</body>
</html>
<html>
<body>
<script
type="text/vbscript">
sometext="Welcome
to our Web Site!!"
document.write(Mid(sometext,1,
3))
</script>
</body>
</html>
No comments:
Post a Comment