An introduction to VBScript, one of the languages used to write ASP pages, and tutorial for beginners.
ASP stands for Active Server Pages, and is a script engine that runs on IIS web servers (or on Apache servers using ChiliASP). Both IIS and ASP are developed by Microsoft. ASP pages can be written in any one of several scripting languages, the most common one (and default) being VBScript. ASP is a server-side scripting engine that allows you to add interactivity to web pages, process data, interact with databases and so on. It provides similar functionality to languages like Perl and PHP, and like PHP does not need to run within a cgi-bin.
There are several versions of ASP, the current one being ASP 3.0 (ASP.NET is a part of the .NET platform, and we are talking here about "classic" ASP only). VBScript itself is a scripting language and subset of Visual Basic.
VBScript contains seven built-in objects, and you will usually be working with these when writing ASP pages. The objects themselves are:
For this basic introduction, we need only worry about the Response object. The Response object is responsible for sending information back to a user from the server. Any time you write something to a page using VBScript, you will be using the Response object.
ASP pages always end with the extension ".asp", for example "index.asp". Pages that have this extension, on a properly configured server, will be processed by the ASP scripting engine before being returned to the user. Anything within the tags <% and %> is code and will be processed by the engine.
Which brings us neatly to our first piece of code. The first thing you will learn to do with ASP is use it to write a simple string to a web page (the famous "Hello World"). "A string" is a common name for text data.
<html> <body> <% Response.Write("Hello World!") %> </body> </html>
Copy and paste the above to an empty text file (for example in Notepad). Save it as "vbscript101.asp", and upload it to a server that supports ASP. When you load the page in a browser, you should see the text "Hello World!". If you do, congratulations! You've finished your first script. If not, it may be that your server does not support ASP - the above should work on any server with ASP support.
What you have done in the above example is write out a normal HTML page, but you have used the ASP scripting engine to write out the text within the page. A good start, but not the most useful of tools so far. Next, we'll learn how to use basic variables.
Variables in VBScript are easy to use, but can be confusing at times. It is always a good idea to name your variables so that you can see easily what they refer to. For example, a variable containing a first name might be called "strFirstName". The "str" tells you it is a string, or text, piece of data. The "FirstName" section tells you what the variable contains - in this case a first name. For a year of birth, you might call a variable "intYearOfBirth" - the int telling you this variable contains an integer.
To use these in a script is reasonably simple. To start with, you need to declare the variable - to tell the ASP engine to create the variable - and then assign a value to it. We then write it out to the page, like so:
<html> <body> <% dim strHelloWorld strHelloWorld = "Hello World, Again!" Response.Write(strHelloWorld) %> </body> </html>
Note that when you use the variable with the "Response.Write" section, you should not put quotation marks around it. If you do, the script will just write out "strHelloWorld" as text, rather than writing the value you gave it.
The "dim" line is where you declare the variable. The next line is used to assign the value "Hello World, Again!" to the variable. And the third line is used to write it to the page. If you copy and paste the above as before, and upload the new script and open it in a browser, you should now see the text "Hello World, Again!" on the page. If you do, congratulations, you have just finished your second ASP script.
Still, though, not the most impressive piece of interaction on the web. Next though, we're going to write a more complex script. This time, we're going to create a variable, give it a value of the current hour of the day, and display a piece of text based upon whether it is morning or afternoon.
<html> <body> <% dim intHourOfDay intHourOfDay = hour(now()) if intHourOfDay < 12 then Response.Write("Good Morning World!<br><br>The hour of the day is ") & intHourOfDay else Response.Write("Good Afternoon World!<br><br>The hour of the day is ") & intHourOfDay end if %> </body> </html>
We start this time by declaring a variable, intHourOfDay. The "int" is to remind us later on, if needed, that this variable contains a number, and the "HourOfDay" tells us that the number itself will be the hour of the day. Next, we grab the current time using the "now()" function, and the current hour by using the "hour()" function on the result (the hour function gives us the hour of a date). At this stage, intHourOfDay will contain a number representing the hour of the current day.
Next, we check the value. If the value is less than 12, we know the current time is morning, and write out an appropriate greeting, followed by the hour of the day (you use the & symbol to write more than one thing to a page at once). Otherwise (else) it is afternoon. The "end if" section at the end tells the ASP scripting engine that the "if" section is over, and to carry on from here as normal.
Now, to use this, you simply need to upload this script, as before, to your web server, and open it in a web browser. Your server may be on a different time zone to you, but you can see what hour of the day it thinks it is underneath the greeting. What you should now see is a greeting of either "Good Morning World!" or "Good Afternoon World!" followed by a blank line, then the hour of the day.
And voila - your first piece of interactive web programming. Congratulations, you've taken your first steps in ASP!