VB-Script Notes
VBScript stands for Visual Basic Scripting that forms a subset of Visual Basic for Applications
(VBA). VBA is a product of Microsoft which is included NOT only in other Microsoft products such
as MS Project and MS Office but also in Third Party tools such as AUTO CAD.
Features of VBScript
VBScript is a lightweight scripting language, which has a lightning fast
interpreter.
VBScript, for the most part, is case insensitive. It has a very
simple syntax, easy to learn and to implement.
Unlike C++ or Java, VBScript is an object-based scripting
language and NOT an Object- Oriented Programming language.
It uses Component Object Model (COM) in order to access the
elements of the environment in which it is executing.
Successful execution of VBScript can happen only if it is executed in Host Environment such as
Internet Explorer (IE), Internet Information Services (IIS) and Windows Scripting Host (WSH)
Let us write a VBScript to print out "Hello World".
<html>
<body>
<script language="vbscript" type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>
1)
<script language="vbscript" type="text/vbscript">
var1 = 10
var2 = 20
Sum = var1 + var2
document.write("The Sum of two numbers"&_
"var1 and var2 is " & Sum)
</script>
2)
<html>
<head>
<script type="text/Vbscript">
<!--
Function sayHello()
Msgbox("Hello World")
End Function
//-->
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>
3)
<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim Var1
Dim Var2
Call add()
Function add()
Var1 = 10
Var2 = 15
Dim Var3
Var3 = Var1+Var2
Msgbox Var3 'Displays 25, the sum of two values.
End Function
Msgbox Var1 ' Displays 10 as Var1 is declared at Script level
Msgbox Var2 ' Displays 15 as Var2 is declared at Script level
Msgbox Var3 ' Var3 has No Scope outside the procedure. Prints
Empty
</script>
</body>
</html>
4)
<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim intRadius
intRadius = 20
const pi=3.14
Area = pi*intRadius*intRadius
Msgbox Area
</script>
</body>
</html>
5)
<html>
<body>
<script language="vbscript" type="text/vbscript">
Const myString = "VBScript"
Const myDate = #01/01/2050#
Msgbox myString
Msgbox myDate
</script>
</body>
</html>
6)
<html>
<body>
<script language="vbscript" type="text/vbscript"> Dim a : a = 5
Dim b : b = 10
Dim c
c = a+b
Document.write ("Addition Result is " &c)
Document.write ("<br></br>")
c = a-b
Document.write ("Subtraction Result is " &c)
Document.write ("<br></br>") 'Inserting a Line Break for readability c = a*b
Document.write ("Multiplication Result is " &c)
Document.write ("<br></br>")
c = b/a
Document.write ("Division Result is " &c)
Document.write ("<br></br>")
c = b MOD a
Document.write ("Modulus Result is " &c)
Document.write ("<br></br>")
c = b^a
Document.write ("Exponentiation Result is " &c)
Document.write ("<br></br>")
</script>
</body>
7)
PrimeNumberLimit = Inputbox("Enter the range to check Prime no : ")
'Initialising the list of prime numbers
PrimeNumberList="Prime number list is:"
For Ctr1=1 to PrimeNumberLimit step 2 'check only odd nos
PrimeFlag=True ' Initialising a Flag variable
If Ctr1>=4 Then
For Ctr2=2 to Ctr1/2
If Ctr1 mod Ctr2 = 0 Then 'Checking the reminder
PrimeFlag=False 'Not a prime number
Exit For 'no need to check further
End If
Next
End If
If PrimeFlag=True Then
PrimeNumberList=PrimeNumberList&" "&Ctr1
End If
Next
Msgbox PrimeNumberList
'Displaying the result
8)
MyStr=Ucase(inputbox("Enter the String:"))
RevStr=strreverse(MyStr)
if strcomp(MyStr,RevStr)=0 then
msgbox "It is a Palindrome"
else
msgbox "It is not a Palindrome"
end if
9)
JAVA SCRIPTS EXAMPLES:
Print Prime Numbers
<html>
<head>
<title>JavaScript to print prime numbers between two integers!</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function printPrime() {
"use strict";
var i, j, c, start, end;
//get the start and end value from form
start = parseInt(document.getElementById('start').value);
end = parseInt(document.getElementById('end').value);
//clear the result div
document.getElementById("result").innerHTML = '';
//loop till i equals to end
for (i = start; i <= end; i++) {
c = 0;
for (j = 1; j <= i; j++) {
// % modules will give the reminder value, so if the reminder is 0 then it is divisible
if (i % j == 0) {
//increment the value of c
c++;
}
}
//if the value of c is 2 then it is a prime number
//because a prime number should be exactly divisible by 2 times only (itself and 1)
if (c == 2) {
document.getElementById("result").insertAdjacentHTML('beforeend', i + '<br>');
}
}
}
</script>
</head>
<body>
<h2>JavaScript to print Prime numbers between two integers!</h2>
Start: <input type="number" name="start" id="start" min="1" style="width: 100px;"
value="10" />
End: <input type="number" name="end" id="end" min="1" style="width: 100px;"
value="100" /> <input type="submit" value="Print Prime Numbers" onclick="printPrime()"
name="print" />
<div id="result"></div>
</body>
</html>