Top-Down Code vs. Event Driven Code

We write some code, save it in a script file, and use the Window Script Host to execute the script. The Script Host starts executing at the first line and continues to the last line. If a script file contains some procedure definitions (such as our GetUserName function), then the Script Host will only execute those procedures if some other code calls them. Once the Script Host reaches the last line of code, the lifetime of the script ends.

To-Down programs are very useful for task-oriented scripts. For example, you might write a script to search your hard drive for all the files with the extension .DOC and copy them to a backup directory. Or you might write a script that gets executed every time Windows starts that randomly chooses a desktop wallpaper bitmap file for the session of Windows. Top-Down programming is perfect for these kinds of scripts.

Event driven code is different, and is useful in different contexts. As the name implies, event-driven code only gets executed when a certain "event" occurs. Until that event occurs, the code won't get executed. If a given event does not occur during the lifetime of the script, the code associated with the event won't get executed at all. If an event occurs, and there's no code associated with the event at all, the event is essentially ignored.

Event driven programming is the predominant paradigm in Windows programming. Most Windows programs that you use every day were written n the event driven model. This is because of the graphical nature of Windows programs. In a graphical user interface (GUI), you have all sort of buttons drop-down lists, fields in which to type text, etc. Every time a user clicks a button, chooses an item in a list, or types some text into a field, an event is raised within the script. The person wrote that program may or may not have decided to write code in response to that event.

When a GUI-based program starts, there is almost always some top-down style code that executes first. This code would do things like connect to a database, prompt the user for a name and password, load some settings from a file or the Windows registry, etc. Then a form typically comes up. The form contains the menus, buttons, lists, and fields that make up the user interface of the program. At that point, the top-down style code is done, and the program enters what is knows as wait state. No code is executing at this point. The program is just sitting there, waiting for the user to do something. From there on in, it's all about events.

Lets say the user clicks on a button. Now the program comes to life again. The program raises the "Click" event for the button that the user clicked. The code that is attached t the event starts to execute, performs some operations, and when it's finished, the program returns to its wait state. Until another event occurs, the program just sits there.

As for as VBScript, the event driven model is used in scripting for the World Wide Web. The scripts that run inside of HTML pages are all based on events. One script might execute when the page is loaded. Another script might execute when the user clicks on a button or graphic. These "mini scripts" are embedded in the HTML file, and are blocked out in a syntax very similar to the one we used to define the GetUserName function in some other posts.

Let us see an example. Type the following code into your text editor, save the file with a HTML extention, and then select Open from the File menu in Internet Explorer 4.0 or higher to open the file.


<html>
<head>
<Script language="VBScript">
Sub ButtonClicked
window.alert("You clicked the button.")
End Sub
</Script>
</head>
<body>
<Button name="SomeButton" type="Button" onclick="ButtonClicked">
Click Me
</Button>
</body>
</html>