VBScript: No Semicolons! If you have programmed before, you will be quite accustomed to placing a semicolon at the end of every statement. In VB V B this is unnecessary because a newline symbolizes the end of the statement. This example will print out 3 separate phrases to the browser. Note: There are 0 semicolons!
VBScript Code: document.write("No semicolons") document.write(" were injured in the making") document.write(" of this tutorial!")
Display: No semicolons were injured in the making of this tutorial!
VBScript Accessing Methods 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. So far in this tutorial we have been using the write method that is a part of the document object and the syntax for doing d oing this is: •
document.write(string_here)
VBScript Multiple Line Syntax As we stated above, the syntax is that placing a newline in your VBScript signifies the end of a statement, much like a semicolon would. However, what happens if your code is so long that absolutely must place your code cod e on multiple lines? Well, Microsoft has included a special character for these multiple lines of code: the underscore "_". The following example contains an exceptionally long write statement that we will break up string concatenation con catenation and the multiple line special character. c haracter.
VBScript Code: document.write("This is a very long write " &_
"statement that will need to be placed onto three " &_ "lines to be readable!")
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: Dim myVariable1 Dim myVariable2
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: Dim myVariable1, myVariable2
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 n ame 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: Dim myVariable1, myVariable2 myVariable1 = 22 myVariable2 = "Howdy" document.write("My number is " & myVariable1) document.write("My string is " & myVariable2)
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: Dim myFirstObject SET myFirstObject = New myClass myFirstObject = nothing
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: Dim myString myString = "Hello there!" document.write(myString)
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: Dim myString myString = "Hello there!" myString = myString & " My name" myString = myString & " is Frederick" myString = myString & " Nelson." document.write(myString)
Display: Hello there! My name is Frederick Nelson.
Thank you for interesting in our services. We are a non-profit group that run this website to share documents. We need your help to maintenance this website.