A Good Host
VBScript (officially, "Microsoft Visual Basic Scripting Edition") is what is called a "hosted" language. That means that the code to actually run VBScript must already be built into some other software in advance. By contrast, Visual Basic has what is called a 'runtime' module - the software necessary to run Visual Basic is self contained and can be installed for greater independence from other software. At present, Microsoft provides three 'host' environments for VBScript:
- IE (Internet Explorer) - Microsoft's web browser software
- IIS (Internet Information Server) - Microsoft's web server software
- WSH (Windows Script Host) - A 'host' environment in the Windows operating system
In other words, VBScript works on your end of the web pipe, on the other end of the web pipe, and can automate the internal operation of your computer. For more information about using WSH, try this exclusive About.Com article:
One of the advantages of VBScript (in common with other scripting languages) is that it's written in plain, ordinary ASCII text. That means that your 'development environment' can be something as simple as Notepad. This makes it ideal as part of, for example, an HTML web page.
More sophisticated software for developing scripts certainly is available. Microsoft, for example, offers the 'Script Debugger' which lets you step through VBScript source code, add break points, and other functions. Download it here. In general, HTML editors also support script editing. So before starting a hunt for a script editor, check to see what kind of script support is in your favorite web page editor. Frontpage 2000, for example, supports the Microsoft Script Editor as shown below.
The next page illustrates using VBScript with the same 'Two Button Form' on a web page with VBScript that was introduced in Learning Visual Basic.
Learning Visual Basic
If you're new to Visual Basic, or even new to programming, this is the place! Visual Basic from the very ground up will be explained. Those of you who are more code-wise might find this article slightly boring from a technical perspective ... but Hey!, you're welcome here too!
The first thing you need to know, and I mean know, about programming is that it's not magic and you can do it. The people who have trouble learning to program are the ones who expect to have trouble. If you think it's going to be easy, it probably will be for you. So plan on having fun! It's the best way to learn.
The second thing is to realize is that the computer is really dumb! I mean stupid, idiotic, and just plain dense. That's why it has to be told each and every thing to do ... at some time. The TV and movie plots where the computer is this scheming, thinking, entity are completely wrong. If a computer doesn't have instructions to do something, it will not do anything. Your task as a programmer is to provide those instructions. The flip side of this is that if it's not doing what you intend, then it's because the instructions that it was given were wrong.
The main flaw in this is that YOU don't give the computer all of the instructions. It gets a lot of them from other programmers at other times in the form of software like the compiler (the program that turns a 'programming language' like Visual Basic into instructions that the computer can actually understand), the operating system (the program that turns things like your keystrokes into instructions that the computer can actually understand), and software objects (little packages of self contained program code that do things that a lot of people need). So to really understand the instructions that the computer received, you have to have an idea about what all the other instructions are. Welcome to programming!
The Two Button Form looks like this in the IE 6 web browser.
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 6. Here's the VBScript code:
Sub buttonA_onclick
MsgBox("Button A was clicked")
End Sub
Sub buttonB_onclick
MsgBox("Button B was clicked")
End Sub
The Compatibility Conundrum
As the previous page noted, execution of this code depends on a suitable 'host'. IE works! IE will also host JavaScript. Netscape, on the other hand, will host JavaScript but it will not host VBScript. For this reason, the standard procedure for most coders today is to use Javascript as the client scripting language rather than VBScript even in true blue Microsoft shops. If you can guarantee what type of browser will be used (for example, in a corporate Intranet) or if you just don't care, go ahead and use VBScript anyway.
As it turns out, this isn't as much of a problem as it used to be. Whether you agree with it or not, IE has pretty much taken over the entire browser market.
The Complete HTML Source
In any case, this remains a good way to introduce VBScript coding. Let's look at the entire HTML file.
Things to notice:
- 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. This tells the browser which host script interpreter to use.
- The INPUT tag. This is the DHTML tag that links the script code to a browser object.
- The script code is in the HEAD section of the HTML rather than the BODY. When the browser finds the reference in the INPUT tag in the body, the code in the HEAD has already been read.
- The names of the script subroutines are the same as in Visual Basic: object name, underscore character, event.
<html>
<head>
<title>Two Button Form</title>
<SCRIPT ID=clientEventHandlersVBS 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="A" id=buttonA name=buttonA>
<INPUT type="button" value="B" id=buttonB name=buttonB>
</body>
</html>
Source http://visualbasic.about.com
Reviews:
Post a Comment