VBScript: No Semicolons!
VBScript: No Semicolons!
If you have programmed before, you will be quite accustomed to placing a semicolon at If you have programmed before, you will be quite accustomed to placing a semicolon at the end of every statement. In V
the end of every statement. In VB this is unnecessary because a newline symbolizes theB this is unnecessary because a newline symbolizes the end of the statement. This example
end of the statement. This example will print out 3 separate phrases to the browser.will print out 3 separate phrases to the browser.NoteNote:: There are 0 semicolons!
There are 0 semicolons!
VBScript Code:
VBScript Code:
<script type="text/vbscript"> <script type="text/vbscript"> document.write("No semicolons") document.write("No semicolons")document.write(" were injured in the making") document.write(" were injured in the making") document.write(" of this tutorial!")
document.write(" of this tutorial!") </script>
</script>
Display:
Display:
No semicolons were injured in the
No semicolons were injured in the making of this tutorial!making of this tutorial!
VBScript Accessing Methods
VBScript Accessing Methods
To access methods and properties contained within an object, like the Document object, To access methods and properties contained within an object, like the Document object, you use a period "." after the object and before the name of the method.
you use a period "." after the object and before the name of the method. So far in this tutorial we have been
So far in this tutorial we have been using theusing the writewrite method that is a part of the method that is a part of the documentdocument object and the syntax for d
object and the syntax for doing this is:oing this is: •
• document.write(string_here)document.write(string_here)
VBScript Multiple Line Syntax
VBScript Multiple Line Syntax
As we stated above, the
As we stated above, the syntax is that placing a newline in syntax is that placing a newline in your VBScript signifies theyour VBScript signifies the end of a statement, much like a semicolon would. However, what happens if your code is end of a statement, much like a semicolon would. However, what happens if your code is so long that absolutely must place your cod
so long that absolutely must place your code on multiple lines?e on multiple lines? Well, Microsoft has included a special character
Well, Microsoft has included a special character for these multiple lines of code: thefor these multiple lines of code: the underscore "_". The following example contains an exceptionally long
underscore "_". The following example contains an exceptionally long writewrite statementstatement that we will break up string con
that we will break up string concatenation and the multiple line special ccatenation and the multiple line special character.haracter.
VBScript Code:
VBScript Code:
<script type="text/vbscript"> <script type="text/vbscript">
document.write("This is a very long write " document.write("This is a very long write " &_&_
"statement that will need to be placed onto three " &_ "lines to be readable!")
</script>
Display:
This is a very long write statement that will need to be placed onto three lines to be readable!
VBScript Syntax Review
VBScript is a client-side scripting language that is based off of Microsoft's Visual Basic programming language. No semicolons are used at the end of statements, only newlines are used to end a statement.
If you want to access methods of an object then use a period "." and an underscore is used for statements that go multiple lines!
VBScript Variables
Variables in VBScript behave just like those variables you would use in Visual Basic. If you don't know Visual Basic don't worry because we are going to put you through Tizag's Five Minute VBScript Variable Bootcamp.
For those of you who have not used variables before, then you should know that the notion of variables helps to make programs easier to program and to understand.
Variables are used to hold information so that it can be reused throughout the program. You can think of variables as a grocery bag and your information you want to store as your groceries. Without the bag it's going to be a pain the bum to drag all that food around!
Declaring Variables in VBScript
When you are going to use a variable in your VBScript code you should first declare it. To declare a variable in VBScript you use the Visual Basic Dim statement:
VBScript Code:
<script type="text/vbscript"> Dim myVariable1
Dim myVariable2 </script>
Display:
Nothing is displayed because we were simply setting up our variables for use!
Note: It is also possible to declare multiple variables in one line. Use the comma "," to separate your multiple variable names.
VBScript Code:
<script type="text/vbscript"> Dim myVariable1, myVariable2 </script>
Display:
VBScript Variables: Setting Values
When you want to set your variable equal to a value you use the equals character "=", also known as the equals operator. The name of your variable appears on the left of the equals, while the value you are setting your variable equal to appears to the right of the equals.
To use your variable, simply type the variable name where you would like to use it. In this example we create two variables, set set one equal to a number and the other equal to a string. We then use the document.write method to print both out to the browser.
VBScript Code:
<script type="text/vbscript"> Dim myVariable1, myVariable2 myVariable1 = 22
myVariable2 = "Howdy"
document.write("My number is " & myVariable1)
document.write("<br />My string is " & myVariable2) </script>
Display:
My number is 22 My string is Howdy
VBScript Variables: Setting Objects
Setting Objects? Yes, VBScript allows you to use objects in your co de. Objects are blobs of code goo that can contain multiple methods and variable. When you want to use these
methods and variables you have to use the period operator "." to gain access to them. See our VBScript Syntax lesson for more information.
When you are setting a value to a variable that is actually an object then you must follow some special syntax. The SET keyword lets VBScript know that you are setting your variable equal to an object. In addition to this, you also have to set the variable equal to nothing after you are finished with it! It's a lot of extra work, but it is necessary.
This example shows how to use a fictional class myClass to create an object. For this example to work you would have to create myClass yourself. Going into more detail is behind the scope of this lesson.
VBScript Code:
<script type="text/vbscript"> Dim myFirstObject
SET myFirstObject = New myClass myFirstObject = nothing
</script>
Display:
Note: Once again, nothing is displayed because we were just showing you how to set and release an object in VBScript!
VBScript Strings
Strings are a bunch of alpha-numeric characters grouped together into a "string" of characters. This sentence I am writing right now is an example of a text string and it could be saved to a VBScript string variable if I wanted it to.
VBScript String Syntax
To create a string in VBScript you must surround the letters or numbers you wish to be stringanized (that's a made up Tizag word) with quotations, like this:
• "Hello I am a string!"
VBScript String: Saving into Variables
Just like numeric values, strings in VBScript are saved to v ariables using the equal operator. This example shows how to save the string "Hello there!" into the variable myString and then use that variable with the document.write function.
VBScript Code:
<script type="text/vbscript"> Dim myString
myString = "Hello there!" document.write(myString) </script>
Display:
Hello there!
VBScript String Concatenation
Often it is advantageous to combine two or more strings into one. This operation of adding a string to another string is referred to as concatenation. The VBScript script concatenation operator is an ampersand "&" and occurs in between the two strings to be joined. This example will join a total of 4 strings to form a super string.Note: We only use one variable in this example!
VBScript Code:
<script type="text/vbscript"> Dim myString
myString = "Hello there!"
myString = myString & " My name"
myString = myString & " is Frederick" myString = myString & " Nelson."
document.write(myString) </script>