Sunteți pe pagina 1din 9

VBScript Examples

Writing output:

first.html

<html>
<body>
<script type = "text/vbscript">
document.write ("<h1><center>Generic Solutions</h1></center> ")
</script>
</body>
</html>

Using External VBScript file:

ext.vbs , first1.html

ext.vbs

document.write("<h1><center>Generic Solutions</center></h1>")
document.write("<h2><center>Plano,TX</center></h2>")

first1.html
<html>
<head>
<script type="text/vbscript" src="ext.vbs">
</script>
</head>
<body>
</body>
</html>

Using Variables and Arrays:

ext.vbs

Option Explicit
document.write("<h1><center>Generic Solutions</center></h1>")
document.write("<h2><center>Plano,TX</center></h2>")
Dim Batch
Dim Student(3)
Batch = "Batch1"
Student(0) = "Lavanya"
Student(1) = "Jayashri"
Student(2) = "Meena"
document.write("Batch name is " & Batch & "<br />")
document.write("List of students <br />")
document.write(Student(0) &"<br />")
document.write(Student(1) &"<br />")
document.write(Student(2) &"<br />")

Dynamic Arrays:

<html>
<head>
<script type="text/vbscript">

Dim array_dynamic()
ReDim array_dynamic(2)
array_dynamic(0) = 1
array_dynamic(1) = 2
array_dynamic(2) = 3

ReDim Preserve array_dynamic(5)

MsgBox array_dynamic(2)

</script>

</head>
<body>
</body>
</html>

Conditional Statements:
first1.html

<html>
<head>
<script type="text/vbscript" src="ext.vbs"></script>
<script type="text/vbscript" src="cond.vbs"></script>
</head>
<body>
</body>
</html>

Cond.vbs

Dim d
d=weekday(date)
If d=4 then
document.write("VBScript Class today at 6.00 PM")
End If

Cond.vbs
Dim d
d=weekday(date)

If d=4 then
document.write("VBScript Class today at 6.00 PM")
ElseIf d=5 then
document.write("Operational Management Class at 6.00 PM")
Else
document.write("No Class today")
End If

Cond.vbs

Dim d
d=weekday(date)

Select Case d
Case 2
document.write("XMl Class")
Case 3
document.write("EDI Class")
Case 4
document.write("VBScript Class")
Case else
document.write("No Class")
End Select
Looping For ----- Next Loop , Do --- Loop ,

Ext.vbs

Option Explicit

document.write("<h1><center>Generic Solutions</center></h1>")
document.write("<h2><center>Plano,TX</center></h2>")

Dim Batch , i
Dim Student(3)
Batch = "Batch1"
document.write("Batch name is " & Batch & "<br />")
document.write("List of students <br />")

Student(0) = "Lavanya"
Student(1) = "Jayashri"
Student(2) = "Meena"

For i = 0 To 2
document.write(Student(i) &"<br />")
Next

Procedures and Functions:

<html>
<body>
<script type="text/vbscript">

Sub ConvertTemp()

temp = InputBox("Please enter the temperature in degrees F",1)


msgbox "The temperature is " & Celsius(temp) & "degrees C"
End Sub

Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
Call ConvertTemp()
</script>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
<script type="text/vbscript">

call square()

Function do_square(number)
do_square = number * number
End Function

Sub square()
var1 = InputBox("Please enter a number")
MsgBox ("The square is " & do_square(var1))
End Sub
</script>

</head>
<body>
</body>
</html>
Pass by Value and Pass by reference:

<html>
<head>
<script type="text/vbscript">

Sub mySub()
MsgBox "hI"
Dim X
X=2
document.write("in mysub" & X)
Call newsub(x)
document.write("Back in mysub" & X)
End Sub

Sub newSub(ByVal var1)


var1 = var1 * var1
document.write ("in newsub" & var1)
End Sub
</script>
</head>
<body>
<script type = "text/vbscript">

Call mySub()

</script>
</body>
</html>
Chaange ByVal to ByRef and see the difference in output.
--------------------------------------------------------------------------------------------------------------------------------------
VBScript for some validation:
Eg:1

<html>
<head>
<script type="text/vbscript">

Sub DisplayData()
If Len(Document.InputForm.firstname.Value) > 5 Then
MsgBox "You have entered too many characters! You need fewer characters before I will submit this form"
End If
End Sub

</script>
</head>
<body>
<form name ="InputForm">
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
<button onclick="DisplayData()">OK</button>
</form>
</body>
</html>
Eg:2

<HEAD>
<TITLE>Working With VBScript</TITLE>
<script type="text/vbscript">
Option Explicit
Sub cmdSubmit()

If (Len(document.InputForm.txtAge.value) = 0) Then

MsgBox "You must enter your age before submitting."

Exit Sub

End If

If (Not(IsNumeric(document.InputForm.txtAge.value))) Then

MsgBox "You must enter a number for your age."

Exit Sub

End If

Msgbox "Thanks for providing your age"

End Sub

</SCRIPT>
</HEAD>
<BODY>

<H1>A VBScript Example </H1>

<P> This example demonstrates validation techniques in VBScript. </P>

<FORM NAME="InputForm">

Enter your age: <input type="text" name="txtAge" /><br />

<button onclick="cmdSubmit()">OK</button>

</FORM>
</BODY>
</HTML>

VBScript Classes

Class Hello_World
Public Sub Say_Hello(Name)
MsgBox "Hello, " & Name & ", welcome to " & Garden & "."
End Sub
Public Garden
End Class

Dim MyHello_World
Set MyHello_World = New Hello_World
MyHello_World.Garden = "Fountain"
MyHello_World.Say_Hello "Sachin"
Property Let will allow code outside of the class to assign a value to a private
variable of the class.

Property Get will allow code outside of the class to read the value of a private
property variable.

Class welcome
Private ur_name
Public Property Let Name(var_name)
ur_name =var_name
End Property

Public Sub Showwelcome(var_type)


MsgBox Makewelcome(var_type) & ur_name & "."
End Sub
Private Function Makewelcome(var_type)
Select Case var_type
Case "Formal"
Makewelcome = "welcome, "
Case "Informal"
Makewelcome = "Hello there, "
Case "Casual"
Makewelcome = "Hey, "
End Select

End Function
End Class

Dim my_object
Set my_object = New welcome
With my_object
.Name = "sachin"
.Showwelcome "Informal"
.Showwelcome "Formal"
.Showwelcome "Casual"

End With
Set my_object = Nothing

Class_Initialize and Class_Terminate are associated with every class that we


create. Class_Initialize is fired whenever an object based of a class is instantiated.
e.g
Set objectname = New classname

Class_Initialize event's general format is:

Private Sub Class_Initialize( )


'Initalization code goes here
End Sub

The Class_Terminate event is fired when the object goes out of scope, or when
object is set to Nothing.

Class_Terminate event's general format is:


Private Sub Class_Terminate( )
'Termination code goes here
End Sub
This program accepts temperature in F and converts it to C
----------------------------------------------------------

<html>
<body>
<script type="text/vbscript">

Dim temp
Dim Cel

temp = InputBox("Please enter the temperature in degrees F")

Cel = Celsius(temp)

msgbox Cel

msgbox "The temperature is " & Cel & "degrees C"

Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function

</script>
</body>
</html>

This program accepts two values and outputs its product


---------------------------------------------------------

<html>
<body>
<script type="text/vbscript">

Dim A, B , C

A = InputBox("Enter the first number")


B = InputBox("Enter the second number")

C = FunMul(A,B)

msgbox "The product of" & A & "and" & B & "is" & C

Function FunMul(A,B)
FunMul = A * B
End Function

</script>
</body>
</html>
-------------------------------------------------------------------
This program accepts two values and outputs its product
<html>
<body>
<script type="text/vbscript">

Dim A, B , C

Sub AcceptValues()

A = InputBox("Enter the first number")


B = InputBox("Enter the second number")
C = FunMul(A,B)
msgbox "The product of" & A & "and" & B & "is" & C

End Sub

Function FunMul(A,B)
FunMul = A * B
End Function

Call AcceptValues()
Call AcceptValues()

</script>
</body>
</html>

--------------------------------------------------------------------
Same program as above but uses for loop to execute procedure 2 times
<html>
<body>
<script type="text/vbscript">

Dim A, B , C , i

Sub AcceptValues()

A = InputBox("Enter the first number")


B = InputBox("Enter the second number")
C = FunMul(A,B)
msgbox "The product of" & A & "and" & B & "is" & C

End Sub

Function FunMul(A,B)
FunMul = A * B
End Function

for i = 1 to 2
Call AcceptValues()

Next

encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition

S-ar putea să vă placă și