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 Does it Work?
• When a VBScript is inserted into a
HTML document,
• the Internet browser will read the
HTML and interpret the VBScript.
• The VBScript can be executed
immediately, or at a later event.
How to Put VBScript Code in an
HTML Document
• <html>
• <head>
• </head>
• <body>
• <script type="text/vbscript">
• document.write("Hello from VBScript!")
• </script>
• </body>
• </html>
• To insert a script in an HTML document,
use the <script> tag.
• Use the type attribute to define the
scripting language.e.g
• <script type="text/vbscript">
• Then comes the VBScript: The command
for writing some text on a page is
document.write:
document.write("Hello from VBScript!")
• The script ends:
• </script>
Head section
• Scripts can be placed in the head
section. Usually we put all the
"functions" in the head section.
• The reason for this is to be sure
that the script is loaded before the
function is called.
Body section
• Execute a script that is
placed in the body section.
• Scripts in the body section
are executed when the page
is loading.
Where to Put the VBScript
• Scripts in a page will be executed
immediately while the page loads into the
browser.
• This is not always what we want.
• Sometimes we want to execute a script
when a page loads, other times when a
user triggers an event.
• <html>
• <head>
• <script type="text/vbscript">
• some statements
• </script>
• </head>
• <html>
• <head>
• </head>
• <body>
• <script type="text/vbscript">
• some statements
• </script>
• </body>
Scripts in both the body and the
head section
• You can place an unlimited
number of scripts in your
document, so you can have
scripts in both the body and
the head section.
• <html>
• <head>
• <script type="text/vbscript">
• some statements
• </script>
• </head>
• <body>
• <script type="text/vbscript">
• Document.write(“hai”)
• </script>
• </body>
• </html>
Create a variable
• <html>
• <body>
• <script type="text/vbscript">
• dim name
• name=“zain”
• document.write(name)
• </script>
• </body>
• </html>
Insert a variable value in a text
• <html>
• <body>
• <script type="text/vbscript">
• dim name
• name=“Lion”
• document.write("My name is: " & name)
• </script>
• </body>
• </html>
Create an array
• <html>
• <body>
• <script type="text/vbscript">
• dim famname(5)
• famname(0)="zain"
• famname(1)="Tiger"
• famname(2)="lalli"
• famname(3)="Stale"
• famname(4)="Jim"
• famname(5)="tom"
• for i=0 to 5
• document.write(famname(i) & "<br >")
• next
• </script>
• </body>
• </html>
VBScript Procedures
• We have two kinds of procedures:
• The Sub procedure and
• the Function procedure.
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 that are passed to it
by a calling procedure
• without arguments, must include an empty
set of parentheses ()
• Sub mysub()
• some statements
• End Sub
• or
• Sub mysub(argument1,argument2)
• some statements
• End Sub
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()
• some statements
• myfunction=some value
• End Function
• or
• Function
myfunction(argument1,argument2)
• some statements
• myfunction=some value
• End Function
Sub procedure
The sub procedure does not return a value.
• <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>
• </body>
• </html>
select statement
• <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
Build in fns
• <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>
Reverse the string
• <html>
• <body>
• <script type="text/vbscript">
• sometext = "Hello Everyone!"
• document.write(strReverse(sometext))
• </script>
• </body>
• </html>
Display the current month and day
• <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>
Date and Time Functions
Display date and time
• <html>
• <body>
• <script type="text/vbscript">
• document.write("Today's date is " & date())
• document.write("<br >")
• document.write("The time is " & time())
• </script>
• </body>
• </html>
• Example
• JavaScript:
<script type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
• VBScript:
<script type="text/vbscript">
<!--
document.write("Hello World!")
'-->
</script>