VBScript is a version of the BASIC programming language designed for the Command Prompt. It's used by administrators and programmers to do things that don't need a graphical user interface to get the work done. This is Part 2 of the tutorial. If you would like to start at the beginning, Click Here For Part 1.
In this part of the VBScript "From the Ground Up" tutorial, we code a web page that is designed to be as simple as possible and still accomplish the goal of making sure that you understand how to write and execute a VBScript program: the Two Button Form.
The Two Button Form looks like this in the IE web browser.
--------
Click Here to display the illustration
--------
Programming the "Two Button Form" in VBScript is actually more an example of DHTML (Dynamic HTML) than it is Visual Basic. The actual VBScript code is almost identical to the same code in VB. Here's the complete HTML web page. The VBScript is in the <head> section.
<html>
<head>
<title>The Two Button Form</title>
<script language="VBScript">
<!--
Sub buttonA_onclick
MsgBox("Button A was clicked")
End Sub
Sub buttonB_onclick
MsgBox("Button B was clicked")
End Sub
-->
</script>
</head>
<body>
<input type="button" value="Button A" name=buttonA>
<input type="button" value="Button B" name=buttonB>
</body>
You can copy this to a text file (using Notepad or any ASCII text editor), save it to a file such as TwoButton.html, double-click the file icon, and get the same result ... IF you're using IE. Which brings us to the next topic.
The Compatibility Conundrum
As noted, you can't run VBScript by itself. It has to be executed in a "host". IE works! But most web browsers don't; they will only host JavaScript, another scripting language similar to VBScript. Because programmers don't want their pages to crash in all the other browsers, Javascript is the client scripting language that virtually everyone uses rather than VBScript. Even Microsoft depends on JavaScript in ASP.NET features. But if you can guarantee what type of browser will be used (for example, in a corporate Intranet) or maybe an application you just write for yourself, go ahead and use VBScript in your web app.
In any case, coding VBScript in a web page is a good way to introduce yourself to VBScript. Here are some things to notice about the TwoButton web page:
- The entire script is surrounded by "Comment" marks to keep it from being interpreted as part of the text in the page. The browser host that executes the script reads past these marks.
- The language="VBScript" attribute in the script tag tells the browser which host script interpreter to use. It would be replaced by "JavaScript" in most web pages.
- The script code is in the head section of the HTML rather than the body. Script code must be logically earlier than the objects that it references.
- The name attribute in the input tag ties the tag to the VBScript code.
- The names of the script subroutines are the same as in Visual Basic: object name, underscore character, event.
This isn't really VBScript, but I should mention that you could also code the HTML part as a <button ...> tag rather than an <input ...> tag, which gives you more options. For example, you can embed a font style with button:
<button style="font: bold 28px Arial" name="whatever">Big text</button>
In part 3 of the VBScript tutorial, we will tackle VBScript in the Windows Scripting Host (WSH). This is the way you will probably run it on your own PC.
Click Here for part 3 of the VBScript tutorial: Running VBScript in WSH.
