Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
89 views

VBscript Examples

The document contains code snippets demonstrating different VBScript concepts like variables, arrays, functions, loops, and conditional statements. It shows how to insert values into text, create arrays, write sub and function procedures, use if/else, select case, loop structures, and work with dates and strings.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

VBscript Examples

The document contains code snippets demonstrating different VBScript concepts like variables, arrays, functions, loops, and conditional statements. It shows how to insert values into text, create arrays, write sub and function procedures, use if/else, select case, loop structures, and work with dates and strings.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Create a variable

Insert a variable value in a text

<html>
<body>

<html>
<body>

<script type="text/vbscript">
Dim firstname
firstname="RIELIT"
document.write(firstname)
document.write("<br />")
firstname="AGARTALA"
document.write(firstname)
</script>

<script type="text/vbscript">
Dim name
name="Jan Egil"
document.write("My name is: " & name)
</script>
</body>
</html>

</body>
</html>
Create an array

<html>
<body>

Sub procedure

<html>

<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>

Function procedure

If...Then..Else statement

<html>

<html>

<head>
<script type="text/vbscript">
Function myFunction()
myFunction = "BLUE"
End Function
</script>
</head>

<head>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i < 10 Then
document.write("Good morning!")
Else
document.write("Have a nice day!")
End If
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>

<body onload="greeting()">
</body>
</html>

If...Then..ElseIf statement

Select Case statement

<html>

<html>

<head>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i = 10 Then
document.write("Just started...!")
ElseIf i = 11 Then
document.write("Hungry!")
ElseIf i = 12 Then
document.write("Ah, lunch-time!")
ElseIf i = 16 Then
document.write("Time to go home!")
Else
document.write("Unknown")
End If
End Function
</script>
</head>

<body>
<script type="text/vbscript">
d=weekday(Date)

<body onload="greeting()">
</body>
</html>

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>

For..Next loop

Looping through the HTML headers

<html>
<body>

<html>
<body>

<script type="text/vbscript">
For i = 0 To 5
document.write("The number is " & i & "<br />")
Next
</script>

<script type="text/vbscript">
For i=1 To 6
document.write("<h" & i & ">This is header " & i &
"</h" & i & ">")
Next
</script>

</body>
</html>

</body>
</html>
For..Each loop

Do...While loop

<html>
<body>

<html>
<body>

<script type="text/vbscript">
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"

<script type="text/vbscript">
i=0
Do While i < 10
document.write(i & "<br />")
i=i+1
Loop
</script>

For Each x In cars


document.write(x & "<br />")
Next
</script>
</body>
</html>

</body>
</html>

Display Date and Time

Display the current month and day

<html>
<body>

<html>
<body>

<script type="text/vbscript">
document.write("Today's date is " & Date())
document.write("<br />")
document.write("The time is " & Time())
</script>

<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>
</html>
Display the days

Uppercase or lowercase characters?

<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>

<html>
<body>

Reverse a string

Round a number

<html>
<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>

<script type="text/vbscript">
sometext = "Hello Everyone!"
document.write(StrReverse(sometext))
</script>
</body>
</html>

<script type="text/vbscript">
txt="Have a nice day!"
document.write(UCase(txt))
document.write("<br />")
document.write(LCase(txt))
</script>
</body>
</html>
Remove leading or trailing spaces from a string

<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>

You might also like