What Is VBScript?
What Is VBScript?
What is VBScript?
VBScript Variables
VBScript Procedures
Sub procedure
Function procedure
A Sub procedure:
Sub mysub(argument1,argument2)
some statements
End Sub
A Function procedure:
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
Conditional Statements
Conditional statements are used to perform different actions for different decisions.
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.
<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>
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:
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:
Output:
Output:
Output:
This is a b
Replace Dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beau
tiful","horrible"))
replace()
Output:
StrComp
StrReverse
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