Datasheet

Chapter 1: A Quick Introduction to Programming
25
When the user begins to do something, the program comes to life again. Suppose the user clicks a but-
ton. The program raises the
Click event for the button that the user clicked. The code attached to that
event starts to execute, performs some operations, and when it’s finished, the program returns to its
wait state.
As far as VBScript is concerned, the event-driven model is used heavily in scripting for the Web. Scripts
that run inside of HTML web pages are all based on events. One script may execute when the page is
loaded, while another script might execute when the user clicks a link or graphic. These “mini scripts”
are embedded in the HTML file, and are blocked out in a syntax very similar to the one you used to
define the
PromptUserName function in the previous section.
An Event-Driven Code Example
As you progress through the second half of this book, the finer points of event-driven programming will
become much clearer to you. However, just so you can see an example at this point, type the following
code into your text editor, save the file with a
.HTM extension, and then load it into Internet Explorer 6
(if you are running Internet Explorer 6/7 and you are running this file off your desktop, you might have
to dismiss some security warnings and allow ActiveX).
<html>
<head>
<title>Simple VBScript Example</title>
<script language=”vbscript”>
Sub ButtonClicked
window.alert(“You clicked on the button!”)
End Sub
</script>
</head>
<body>
<button name=”Button1” type=”BUTTON” onclick=”ButtonClicked”>
Click Me If You Can!!!
</button>
</body>
</html>
Figure 1-8 shows the result of clicking the button on the page. In this case it’s only a message box but it
could be much more.
Coding Guidelines
It’s a really good idea to get into healthy programming habits right from the beginning. As you continue
to hone your programming skills and possibly learn multiple languages, these habits will serve you well.
Your programs will be easier for you and your fellow developers to read, understand, and modify, and
they will also contain fewer bugs.
When you first start writing code, you have to concentrate so hard on just getting the syntax correct for
the computer that it may be easy for you to forget about all the things you need to do in order to make
sure your code is human friendly as well. However, attentiveness early on will pay huge dividends in
the long run.
c01.indd 25c01.indd 25 8/27/07 7:45:26 PM8/27/07 7:45:26 PM