This is intended for non-technical readers.

If it starts making no sense, event loop may provide some helpful background. Either that or I'm just not making any sense. If it seems to be the latter, please let me know where you got bogged down, and I'll try to clarify the writeup.

Life is like this for most computer programs nowadays: Things happen, and the program reacts. You might type text into a word processor, in which case one "thing" "happens" each time you press a key. These "things happening" are often referred to as "events". They're a lot more clearly and sharply defined than events in real life.

What do we mean when we talk about "reacting" to an "event"? There will be some kind of switching mechanism somewhere in the program which looks at each "event" as it happens and decides what to do about it. Sometimes, the programmer has to write that part himself; in other environments, the ugly details are handled already and the programmer just has to fill in a few blanks. In either case, the Right Way to do this it to have the switching in one place, and the actual reactions to the events separated out. When you do that, then for each kind of "event" that may happen, you have one piece of code which "handles" that "event", and does nothing else. Cleverly, we call that an "event handler".

This can get complicated. When you click a button on a web page, it's a lot like this:

The button's own "I've been pushed" handler redraws the button so it looks "pushed", and it also "notifies" its owner (the web browser) that it's been clicked. This is very basic, primitive stuff: The button doesn't know or care who owns it or what purpose it serves.

When the web browser hears that a button on a page has been clicked, it calls its "button clicked" event handler. This handler doesn't care about mind-numbing little details like painting buttons. Its worried about what the button is supposed to accomplish, according to the web page that it belongs to: Does the button submit a form? Is there some JavaScript code attached to the button? The browser looks this stuff up and does the appropriate thing. Thus is the event handled, as far as the browser is concerned.

If there's JavaScript code associated with the button, we call that an "event handler" too: A web page can have "events" happen to it just like the rest of us, and one of those is "somebody clicked such-and-such button". So the browser executes the JavaScript code associated with the button.

It's neat: We start with the very myopic focus of the button itself, which occupies itself with minute and mindless concerns. Then we move out to the browser, which is dealing with more complicated and abstract things. Finally, we end up in the shaky hands of some web page designer who wants to pop up a little box on the screen that says "ur 4 g00b3rh3d!"

The important thing is that none of these three "handlers" knows any details about anything but its own task. They certainly don't worry about who called them: They just do their jobs.

Let's look at an example. We'll make a simple little web page that "handles" just one "event":



<head>
    <title>Duhh!</title>
</head>

<script language="JavaScript">
    //  duh() is a useless but illustrative "event handler".
    function duh() {
        alert( 'ur 4 g00b3rh3d!' );
    }
</script>

<body>

    <p>g00berh3d! goo0b4rhe4d! </p>

    <!-- Text inside these exclamation-point/hyphen things is a 
    "comment", which means the browser doesn't display it. -->

    <!--
    "value" is the text that goes in the button itself, like "sumbit" 
    for example.

    "onclick" tells the web browser what the page designer wants it 
    to do on a "click" event.
    -->
    <input type="button" value="DUHH!" onclick="duh();">

</body>


For the sake of simplicity, we left out some HTML stuff that we really should have attended to, but this will work in any normal browser.

If you paste the example into a file named "goober.html" (or anything with ".html" at the end), you can click on the file to see our example in action. You can then modify it! As your attorney, I advise you to modify it and play with it. Just remember to load it in the browser again every time you change something. Usually you have to reload with the shift key pressed to make sure it really gets reloaded. I'd like to write a brief JavaScript for Non-Programmers tutorial. That'd be fun for all, I think.

(See Wharfinger's excellent voltage followers writeup if you are unfamiliar with this social phenomenom / religion)

Event handlers are the clergy of the voltage followers. Followers of the Charge worship mostly alone. But certain events draw large numbers of them:

Not all such events draw a large current of Followers. "Event handlers" are hypothesised as being charged with the task of stepping up attendance to certain events (usually claimed to hold "high potential") from a trickle to a cascade of Followers. Handlers usually "loop" around a fixed circuit, generating activity in alternating ways. Once a suitable venue is located, they summon the entire Coulombarium to attendance.

Event handlers should not be confused with emitters or collectors.

According to the terms of the "Tesla Interdiction", event handlers are of course forbidden from owning transistors. However, they aren't required to wear Tesla coils.

Log in or register to write something here or to contact authors.