VBScript example

<!– test.html,  a vbscript sample –>

<html>
<head>

<!–define VBScript function in head–>
<script type=”text/vbscript”>
function myfun()
document.write(“<h1>Hello World!</h1>”)
end function
</script>

<script type=”text/vbscript”>
function myfun1()
alert(“Hello World!”)
end function
</script>

<script type=”text/vbscript”>
function myfun2()
myfun2=Date()
end function
</script>

<!–define VBScript function with input arguments and return value–>
<script type=”text/vbscript”>
function myfun3(a,b)
myfun3=a+b
end function
</script>

<script type=”text/vbscript”>
function myfun4(a,b)
document.write(a*b)
end function
</script>

<!–define VBScript sub procedures–>
<script type=”text/vbscript”>
sub mysub()
alert(“Hello World!”)
end sub
</script>

<!–use if statement–>
<script type=”text/vbscript”>
sub mysub2()
Dim i
i=11
if i=10 then
 alert(“i=10”)
 else alert(“i<>10″)
end if
end sub 
</script> 

<script type=”text/vbscript”>
sub greeting()
i=hour(time)
if i < 10 then
 document.write(“Current time is: ” & time & “, <br/> Good morning”)
elseif i > 10 & i < 15 then
 document.write(“Current time is: ” & time & “, <br/> Good Afternoon”)
else
 document.write(“Current time is: ” & time & “, <br/> Have a nice day”)
end if 
end sub 
</script> 

<!–use select case statement–>
<script type=”text/vbscript”>
sub whatday()
d = weekday(date)
Select case d
 case 1
  MsgBox(“Sunday”)
 case 2
  MsgBox(“Monday”)
 case 3
  MsgBox(“Tuesday”)
 case 4
  MsgBox(“Wednesday”)
 case 5
  MsgBox(“Thursday”)
 case 6
  MsgBox(“Friday”)
 case else
  MsgBox(“Saturday”)
end select
end sub 
</script> 

<!–use loop statement–>
<script type=”text/vbscript”>
Option Explicit
Dim i

sub dispNum()
for i = 0 to 10 step 2
 document.write(“The number is ” & i & “<br/>”)
 wait(0.1)
 if i = 8 then exit for
Next
end sub 

function wait(i)
Dim starttime,endtime
starttime = timer
endtime = timer
while(starttime + i > endtime )
endtime = timer
wend
end function

function carname()
Dim cars(2),car
cars(0)=”Volvo”
cars(1)=”Benz”
cars(2)=”BMW”
for each car in cars
 document.write(car & “<br/>”)
Next
end function

</script> 

</head>

<body>

<!–load function when page loads
<!–<body onload = “myfun()”>  –>

<!–load procedure when page loads
<!–<body onload = “mysub()”> –>

<!–load VBScript function defined in head–>

<!– button click–>
<button onclick=”myfun1()”>Click me</button>

<br/>
<!– call a function procedure–>
<button onclick=”mysub()”>Click me</button>

<!– define variables –>
<script type=”text/vbscript”>
Option Explicit
Dim x(2),i
x(0)=1
x(1)=2
x(2)=x(0)+x(1)
document.write(“<p>”)
for i=0 to 2
 document.write(x(i) & “<br />”)
next
document.write(“</p>”)

</script>

<p>
<script type=”text/vbscript”>
document.write(“Today’s date is: “)
document.write(myfun2())
</script>
</p>

<p>
<script type=”text/vbscript”>
document.write(“test myfun(3,5) = “)
document.write(myfun3(3,5))
</script>
</p>

<p>
<script type=”text/vbscript”>
call myfun4(3,5)
document.write(“<br/>”)
myfun4 3,5
</script>
</p>

<p>
<button onclick = “mysub2()”>What’s the value?</button>

<button onclick = “greeting()”>Greeting</button>

<button onclick = “whatday()”>What day is today?</button>

<button onclick = “dispNum()”>Display number</button>
<button onclick = “carname()”>Display cars</button>
</p>

</body>
</html>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: