What is VBScript?
Short Description
Download What is VBScript?...
Description
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? document.write("Hello document.wri te("Hello World!")
Where to Put the VBScript? .... ....
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 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 include an empty set of parentheses ()
•
returns a value by assigning a value to its name Function myfunction(argument1,argument2) some statements
myfunction=some 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 If...Then...Else statement - select one of two sets of lines to execute
•
If...Then...ElseIf 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
Function greeting() i=hour(time) If i < 10 Then document.write("Good document.write("Good morning!") Else document.write("Have document.write("Have a nice day!") End If End Function
Function greeting() i=hour(time) If i = 10 Then document.write("Just document.write("Just started...!") ElseIf i = 11 then document.write("Hungry!") ElseIf i = 12 then document.write("Ah, document.write("Ah, lunch-time!") lunch-time!") ElseIf i = 16 then document.write("Time document.write("Time to go home!") Else document.write("Unknown") End If End Function onload="greeting()">
onload="greeting()">
Select Case
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
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 some code Next
For i=10 To 2 Step -2 some code Next
For Each...Next Loop
Do...Loop
Dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x In cars document.write(x document.write(x & "") Next
Do While i>10 some code Loop
Do some code Loop While i>10
Do Until i=10 some code Loop
Functions VBScript vs JavaScript
VBScript Function
Syntax
InStr
Dim txt,pos txt="This is a beautiful day!" pos=InStr(txt,"his") document.write(pos) Output:
InStrRev
2 Dim txt,pos txt="This is a beautiful day!" pos=InStrRev(txt,"his") document.write(pos)
Javascript Function
Syntax
inde in dexO xOff()
var str="Hello world!"; document.write(str.indexOf("Hello document.write(str.indexOf("Hello") ") + ""); document.write(str.indexOf("Wo document.write(str.indexOf("World") rld") + ""); document.write(str.indexOf("world"));
Output:
LCase
2 Dim txt txt="THIS IS A BEAUTIFUL DAY!" document.write(LCase(txt)) Output:
Left
this is a beautiful day! Dim txt txt="This is a beautiful day!" document.write(Left(txt,11)) Output:
Len
This is a b Dim txt txt="This is a beautiful day!" document.write(Len(txt)) Output:
LTrim
24 Dim txt txt=" This is a beautiful day! " document.write(LTrim(txt)) Output:
RTrim
"This is a beautiful day! " Dim txt txt=" This is a beautiful day! " document.write(RTrim(txt)) Output: " This is a beautiful day!"
toLowerC ase()
var str="Hello World!"; document.write(str.toLowerCase());
Trim
Dim txt txt=" This is a beautiful day! " document.write(Trim(txt)) Output: "This is a beautiful day!" Dim txt txt="This is a beautiful day!" document.write(Mid(txt,1,11 ))
Mid
Output: This is a b Dim txt txt="This is a beautiful day!" document.write(Replace(txt, "beautiful","horrible"))
Replace
replace()
Output: This is a horrible day! Right StrComp StrReverse UCase
toUpperC ase()
var str="Hello world!"; document.write(str.toUpperCase());
Math Functions VBScript Functio n Abs
JavaScript
Example
Function
Example
document.write(Abs(48.4) document.write(Abs(48.4) & "") document.write(Abs(-48.4))
abs(x)
document.write(Math.abs(7.25 document.write(Math.abs(7.25)) + ""); document.write(Math.abs(-7.25) document.write(Math.abs(-7.25) + "");
Cos
Output:
document.write(Math.abs(7.25-10));
48.4 48.4
7.25 7.25 2.75 document.write(Math.cos(3) document.write(Math.cos(3) + ""); document.write(Math.cos(-3) document.write(Math.cos(-3) + ""); document.write(Math.cos(0) document.write(Math.cos(0) + ""); document.write(Math.cos(Math document.write(Math.cos(Math.PI) .PI) + ""); document.write(Math.cos(2*Math.PI));
document.write(Cos(50.0))
coss(x co (x))
Output: 0.964966028492113
Log
document.write(Log(38.25678322 7))
log(x)
Output:
Rnd
3.64432088381777 document.write(Rnd)
random()
document.write(Math.random());
sin(x)
document.write(Math.sin(3) document.write(Math.sin(3) + ""); document.write(Math.sin(-3) document.write(Math.sin(-3) + ""); document.write(Math.sin(0) document.write(Math.sin(0) + ""); document.write(Math.sin(Math.P document.write(Math.sin(Math.PI) I) + ""); document.write(Math.sin(Math.PI/2)); document.write(Math.sqrt(0) document.write(Math.sqrt(0) + ""); document.write(Math.sqrt(1) document.write(Math.sqrt(1) + ""); document.write(Math.sqrt(9) document.write(Math.sqrt(9) + ""); document.write(Math.sqrt(0.6 document.write(Math.sqrt(0.64) 4) + ""); document.write(Math.sqrt(-9));
Output:
Sin
0.7055475 document.write(Sin(47)) Output: 0.123573122745224
Sqr
document.write(Sqr(9))
sqrt(x)
Output: 3
Tan
document.write(Tan(40))
tan(x)
Output: -1.1172149309239
round(x)
pow ( x,y )
document.write(Math.round(0. document.write(Math.round(0.60) 60) + ""); document.write(Math.round(0. document.write(Math.round(0.50) 50) + ""); document.write(Math.round(0. document.write(Math.round(0.49) 49) + ""); document.write(Math.round(-4.40 document.write(Math.round(-4.40)) + ""); document.write(Math.round(-4.60)); 1 1 0 -4 -5 document.write(Math.pow(0,0) document.write(Math.pow(0,0) + ""); document.write(Math.pow(0,1) document.write(Math.pow(0,1) + ""); document.write(Math.pow(1,1) document.write(Math.pow(1,1) + ""); document.write(Math.pow(1,10) document.write(Math.pow(1,10) + ""); document.write(Math.pow(2,3) document.write(Math.pow(2,3) + ""); document.write(Math.pow(-2,3) document.write(Math.pow(-2,3) + ""); document.write(Math.pow(2,4) document.write(Math.pow(2,4) + ""); document.write(Math.pow(-2,4) document.write(Math.pow(-2,4) + ""); 1 0 1 1 8 -8 16 16
Date/Time Functions Function CDate
Date DateAdd DateDiff DatePart DateSerial DateValue Day FormatDateTime Hour IsDate Minute Month MonthName Now Second Time Timer TimeSerial TimeValue Weekday WeekdayName Year
Description Converts a valid date and time expression to the variant of subtype Date Returns the current system date Returns a date to which a specified time interval has been added Returns the number of intervals between two dates Returns the specified part of a given date Returns the date for a specified year, month, and day Returns a date Returns a number that represents the day of the month (between 1 and 31, inclusive) Returns an expression formatted as a date or o r time Returns a number that represents the hour of o f the day (between 0 and 23, inclusive) Returns a Boolean value that indicates if the evaluated expression can be converted to a date Returns a number that represents the minute of the hour (between 0 and 59, inclusive) Returns a number that represents the month of the year (between 1 and 12, inclusive) Returns the name of a specified month Returns the current system date and time Returns a number that represents the second of the minute (between 0 and 59, inclusive) Returns the current system time Returns the number of seconds since 12:00 12 :00 AM Returns the time for a specific hour, minute, and second Returns a time Returns a number that represents the day of the week (between 1 and 7, inclusive) Returns the weekday name of a specified day of the week Returns a number that represents the year
View more...
Comments