back
Functions as Event Handlers
First of all, let's define an event. In programming, an event is something the user does, or something that happens in the program. Events are interactions between the user and the web document. For example, the user clicks on the mouse: that's an event called mouseDown. Moving the mouse over something on the screen is another event called mouseOver. JavaScript defines a great many events that we will explore as we learn more.

JavaScript reacts to the user through what is called event handling. You will learn to write bits of code that perform event handling. These bits of code are called event handlers. Examine this code. [function as an event handler].

Check out how it works. First, we defined what is called a function in the JavaScript contained in the head of the HTML document. The name of this function is checkItOut(). Remember that a function is a like a little program within a program, a machine within a machine--I like to think of it as a Genie in a bottle because you can just call on it and it does your bidding. We define the function within curly braces. Specifically, this function does the job of alerting the user that they need to "check out" what they are about to see on the web. If the user agrees by clicking, then another alert box jumps up and asks them if they are a true player. This second alert box depends on another little function (called a method) that returns a value of TRUE if the user clicks on it. If the user does click, a new window opens up with the new website available to the user.

All of the above took place within the head of the HTML document. Now, we go into the body of the document and callthe function. That's how it works.

Assignment

Create two web pages. The first page needs to have a JavaScript function that utilizes what you learned in today's lesson. The second page is the page that opens up after the user's interaction (as in my example today).