-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

ASP.NET AJAX advice
Visual illustrated complete normilization database
asp application




 Lesson 5: Learn VBScript!
Lesson 5: Learn VBScript!

Author: Alexander Haneng
Difficulty: Beginner
Requires: ASP
Demo: Demo1, Demo2,
Demo3, Demo4
Download: lesson5.zip
Summary:
A easy intro to VBScript.

Intro:
VBScript is the default scripting language for ASP, and most of the materials published here are things scripted in VBScript. VBScript is a light version of Visual Basic, and has an easy syntax.

<% and %>
In HTML you use < and > around the tags. In VBScript you use <% and %>. But you can put many tags inside one pair of <% and %>. To print a variable you can use <%=variablename%>.

Variables:
You don't have to explicitly declare your variables before use in VBScript, and all variables is the same type (variant).

My first ASP page:
<%
MyVar = "Hello world!"
%>
<HTML>
<BODY>
<%=MyVar%>
</BODY>
</HTML>
This page displays the word Hello world! Click here to see it.
What I do is assigning the text Hello world to the variable MyVar. Then I use HTML to make a HTML page. Then I use <%=MyVar%> to print out the value of the variable MyVar. And at last I end the HTML page. Pretty easy, right?!

My second ASP page:
This time we will add a little FOR loop to the page:
<%
MyVar = "Hello world!"
%>
<HTML>
<BODY>
<%FOR i=1 TO 100%>
<%=MyVar%>
<%NEXT%>
</BODY>
</HTML>
This page is just the same as the above. The difference is that it prints Hello world! 100 times. Take a look!

My third ASP page:
And then we will add the time:
<%
MyVar = "Hello world!"
%>
<HTML>
<BODY>
The time is: <%=time%> <BR>
<%FOR i=1 TO 100%>
<%=MyVar%>
<%NEXT%>
</BODY>
</HTML>
The word time is a function. It's like a predefined variable containing the current time. To view this example click here. VBScript have about 90 different functions, and they have many uses. Take a look in our index to see them all. You also has Statements, Operators, Objects and Constants, but that's another lesson. (The FOR loop is a statement).

A smart page!
This script will display 'Good day' and 'Good night' according to the time of the day:
<HTML>
<BODY>
<%IF Hour(time)>23 OR Hour(time)<6 THEN%>
Good night!
<%ELSE%>
Good day!
<%END IF%>
</BODY>
</HTML>
Maybe you now see some of the potential of ASP. The page will display 'Good night!' if the hour is between 11pm and 6am, and 'Good day' rest of the day. Try it here! The condition of the IF THEN statement could be anything, from browser type, the visitors IP-address, time of year (X-mas, Easter etc.), and so on. (Please note that the ASP is run on the server so it checks with the current time on the server. The current time on this server is 1:00:58 PM.)

For those of you who knows JavaScript, may not be so impressed, and of course you can do this in JavaScript. But remember that all the processing is done on the server, and only plain vanilla HTML is outputted, making it compatible with almost any modern browser. (Netscape 1.0 and up, or something). No more problems with Document.Write in nested tables in Netscape 3.0 or with those who have disabled JavaScript in their browsers.

A last comment:
I have only scratched the surface of VBScript. But check out our index of VBScript functions, and play around. And when you have made some pages, take a look at the more advanced lessons. The best way to learn, is going through code that others have written. I hope this could help you a little bit into the mysteries of VBScript, and good luck!


4: Add an ASP guestbook!
6: 5 tricks with VBScript!
| Info |
© Copyright 1997-2008 Alexander Haneng