A direct descendant of Visual Basic, VBScript is a scripting language now in its fifth full version. Despite the announcement this year that no new functionality will be added to it, the eleven-year-old language is still useful for a variety of tasks. This article will introduce you to some of the things you can do with it.
Contributed by James Payne Rating: / 6 December 03, 2007
The year was 1996 and Visual Basic sat on a couch watching an episode of The Young and the Restless, stuffing her face with bon-bons. Victor had just proposed to Cassandra, who, unbeknown to Victor, was Victor's sister's third cousin, who was suffering from a horrible case of amnesia, and was pregnant with Victor's fourth-dimensional future self from the year 9000, who it turns out, had also impregnated Victor's own mother, making Victor his own father.
They were just about to reveal all of this to Victor, who himself was suffering from a rare form of bulimia, when Visual Basic felt a slight cramping in her stomach. The baby, whom she had decided to name Visual Basic Script after its father (who shall remain nameless), wasn't due for another month.
But nature cannot be stopped, and twenty four hours later, Visual Basic Script, or VBScript as he would come to be known, was born.
VBScript began life as a limited version scripting language of Visual Basic, targeted at web developers. Within two years of its release, it went from version 1.0 to 5.0 and began to be used by system administrators the world over. In version 5.0, it gained considerable power as Regular Expressions (RegEx), Classes, the With Statement, and some other functionality was added to its repertoire. In 2007 it was noted that no new functionality would be added to the scripting language, though support will still be available as long as Microsoft owns the world.
So now that we got all the boring stuff out of the way, put away your pillow and open up that case of Jolt Cola. We're about to start the grand tour.
Before we begin discussing VBScript I have to ask you some questions though *shines a bright light in your eyes*:
Where were you on the night of October fifteenth?
Do you have a good understanding of HTML?
Do you have a passing knowledge of scripting languages?
Do you have a Notepad? (Not the kind you bring to school...the text editor on your computer)
Even if you didn't answer yes to any of those, I am going to let you read on, because let's face it 1) I need the readership and 2) I think maybe you are smart enough to proceed without all of that, though it certainly wouldn't hurt, particularly the HTML, since that is where we will be placing our VBScript.
If you have ever read anything about any programming languages they give you that really dumb "Hello World" program. We're not going to do it. The world can go find someone else to say hello to for all we care. So let's write a real program, a man's program, a man's man type of program, one that chews tobacco, smokes beer, and drinks nicotine:
<html>
<body>
<script type="text/vbscript">
document.write("Angelina Jolie leave that nerd Brad Pitt.")
</script>
</body>
</html>
Okay Einstein, let's see if you can guess what the output would be. Give up? Well this is what that little snippet of code would have printed out:
Angelina Jolie leave that nerd Brad Pitt.
In the above code, you will notice several things:
We wrote VBScript inside our HTML (hence me suggesting you go and learn it if you don't already know it).
You must use the <script type="text/vbscript"> in your code. This is what tells your browser what language you are speaking in. Without it, it will think you are speaking Swahili.
The way we write text to the browser is with document.write.
Another thing you may have noticed if you are familiar with other programming languages is that you do not end statements with the semi-colon (;). In VBScript, the end of a statement is when you start a new line. This, of course, can be somewhat problematic as you could guess. Let's say we wanted to say more to Angelina. Well, we couldn't simply just type on forever. We could do this:
<html>
<body>
<script type="text/vbscript">
document.write("Angelina Jolie leave that nerd Brad Pitt.")
document.write(" What does he have that I don't?")
document.write(" Aside from looks, money, and a six-pack of abs.")
document.write(" I have a six pack of Mountain Dew...")
</script>
</body>
</html>
This would print out
Angelina Jolie leave that nerd Brad Pitt. What does he have that I don't? Aside from looks, money, and a six pack of abs. I have a six pack of Mountain Dew...
You could also achieve the same affect using concatenation (or joining) "&" and the multiple line special character (the underscore"_"). Here it is in code.
<html>
<body>
<script type="text/vbscript">
document.write("Angelina Jolie leave that nerd Brad Pitt." &_
" What does he have that I don't? " &_
"Aside from looks, money, and a six-pack of abs. " &_
"I have a six pack of Mountain Dew...")
</script>
</body>
</html>
This code results in the same thing as our above code:
Angelina Jolie leave that nerd Brad Pitt. What does he have that I don't? Aside from looks, money, and a six pack of abs. I have a six pack of Mountain Dew...
Variables are containers that you store data in. In that sense, they're similar to a box. You can put data in them, you can take data out and put it back in, you take data out and put new data in, or you can delete the data within them. They are called variables because the data within them varies.
Before you can use any variables, you must declare them. You do so in VBScript with the Dim statement. Here, we will declare some variables and give them a value:
<html>
<body>
<script type="text/vbscript">
Dim firstVariable
Dim secondVariable
firstVariable="13"
secondVariable="James"
</script>
</body>
</html>
The above code creates two variables, one named firstVariable and the other named secondVariable. We then store data in each one (13 and James respectively). Now let's do something with those variables:
<html>
<body>
<script type="text/vbscript">
Dim firstVariable
Dim secondVariable
firstVariable = 13
secondVariable = "James"
document.write("My IQ is " & firstVariable)
document.write("<br />My name is " & secondVariable)
</script>
</body>
</html>
The above code would print the following to the browser:
My IQ is 13
My name is James
You can also declare variables and use them in this manner:
<html>
<body>
<script type="text/vbscript">
Dim firstVariable, secondVariable
firstVariable = 13
secondVariable = "James"
document.write("My IQ is " & firstVariable)
document.write("<br />My name is " & secondVariable)
</script>
</body>
</html>
You get the same result, but save space by declaring the variables on the same line.
Arrays are similar to variables, except where variables hold a value, arrays hold many values. You can think of a variable as a file folder, and an array as a file cabinet. Here is how we declare an array and assign values to it:
<html>
<body>
<script type="text/vbscript">
Dim cow(3)
cow(0) = "Moo "
cow(1) = "Went "
cow(2) = "the "
cow(3) = "cow"
document.write(cow(0))
</script>
</body>
</html>
The above code would result in:
Moo
This is because we told the program to write out the first value in the array, which was moo. You will note that when we Dimmed the array we called "cow," and we put the number 3 in parentheses. This sets the amount of elements, or values, we are storing in the array. I know I know...there are four values in the array, so why did we type three? We do this because array element indexes (the position of the data) begin at 0.
If we had written Dim cow(2), there would be three values, etc.
If we wanted to print out all of the values in the array, we would have to use a For Loop; we will cover this in a later tutorial in more depth. For now, check out the example shown below:
<html>
<body>
<script type="text/vbscript">
Dim cow(3)
cow(0) = "Moo "
cow(1) = "Went "
cow(2) = "the "
cow(3) = "cow"
For Each present In cow
document.write(present)
document.write("<br /><br />")
Next
</script>
</body>
</html>
This would result in the follow being printed to your monitor:
Moo
Went
the
cow
Note that if we had not inserted the <br /> tags, it would have printed...
Moo Went the cow
Lifetime of Variables
A variable's scope is its lifetime, or how long the variable is able to be used. If you put a variable inside a procedure (more on this later), it is only available inside that procedure and when the program exits the procedure, the variable is destroyed. This is known as a local variable. It cannot be used by any other procedures in the program.
A variable declared outside of a procedure is available to the entire program and only dies when the page is closed.
A Final Note on Variables
There are also things called multidimensional arrays, but they are beyond the scope of this particular article. I will discuss them in the future however. For now, just know that they exist.
Well, that is it for this tutorial. Be sure to join me for the next episode when we will cover Statements and Loops if time permits.