headings, lists, tables, paragraphs, links, images.Of course, it includes some text formatting features, too, like
So, many people want to create dynamic or interactive pages that can change in response to the user. Many possibilities:
First, if we look at an example of JavaScript (click here), we see that it has some resemblance to English -- it's certainly readable by humans, at least. This presents a problem: can computers understand this piece of text? No, not really: they can execute instructions, but only a very limited set of instructions in very very specific form (in fact, computer instructions are just sequences of 0's and 1's.
So, we type in this JavaScript program which we can read, but the computer only understands 0's and 1's. So how is the computer able to execute our program?
There are essentially two answers to this question:
certainly many things we do in "real life" could be described algorithmically, but it's usually not how people think about their lives-- nor is it necessarily the best way to think about many things
But computers are, of course, designed to solve problems by taking many steps, so this is clearly an important way of thinking to be familiar with when dealing with computers.
JavaScript programs have to respond to the user: moving the mouse, clicking things, maybe typing some stuff into boxes, etc. Programming that is done to work in this context is usually called Graphical User Interface programming, or GUI programming.
Most JavaScript programming has this flavor, and in order to be able to do a little bit of it, there are a few concepts we're going to need.
In a GUI setting, the program generally waits for the user to tell it
what to do (by, say, clicking a button). These actions are called
events. Most of our JavaScript will be written to respond to
events. Here's a partial list of the events that JavaScript programs might
find interesting (obviously, not every web page you create will deal with
all these events -- but it could if you wanted it to). You don't need
to memorize this table in any sense -- just look over it to get a
sense of what an "event" might be. In the left column is the name of the
event; in the center column is the name of the event handler --
this is how we indicate the JavaScript code that will respond to the
particular event. And in the right column is a little description of the
event is.
| Event | Event handler | Description |
| Click | onClick | Executes JavaScript code when an object on a form is clicked. |
| DblClick | onDblClick | Executes JavaScript code when the user double-clicks a form element or a link. |
| DragDrop | onDragDrop | Executes JavaScript code when the user drops an object onto the browser window, such as dropping a file. |
| KeyDown | onKeyDown | Executes JavaScript code when the user depresses a key. |
| KeyPress | onKeyPress | Executes JavaScript code when the user presses or holds down a key. |
| KeyUp | onKeyUp | Executes JavaScript code when the user releases a key. |
| MouseDown | onMouseDown | Executes JavaScript code when the user depresses a mouse button. |
| MouseMove | onMouseMove | Executes JavaScript code when the user moves the cursor. |
| MouseOut | onMouseOut | Executes JavaScript code each time the mouse pointer leaves an area (client-side image map) or link from inside that area or link. |
| MouseOver | onMouseOver | Executes JavaScript code once each time the mouse pointer moves over an object or area from outside that object or area. |
| MouseUp | onMouseUp | Executes JavaScript code when the user releases a mouse button. |
| Move | onMove | Executes JavaScript code when the user or script moves a window or frame. |
| Reset | onReset | Executes JavaScript code when a user resets a form (clicks a Reset button). |
| Resize | onResize | Executes JavaScript code when a user or script resizes a window or frame. |
| Submit | onSubmit | Executes JavaScript code when a user submits a form. |
Most commonly, we want to deal with events like Click or DblClick; we'll also see that MouseOver and MouseOut can let us do interesting things. We're almost to the point where we can look at an example, but first:
In JavaScript, each of these things (buttons, textboxes, windows, etc.) is an object. The concept of object can actually get very complicated, so for now we'll take a simplified view of objects. For our purposes now, an object consistsof two things:
Now, to change the message in the window's status bar, we need to
use the object corresponding to the window which, not surprisingly, is
called window. The window object has many properties;
we want to change the value of the message that is displayed in the status
bar; the property corresponding to that is the status property.
Note the format of using a "." (just a period) to separate the name of
the object from the name of the property we're interested in. This
variation of the example shows that we can deal with several events
at once -- we use the MouseOut event to change the status bar
message. (Note: depending on your browser, the message you see when your mouse "leaves" the link may only flash very briefly -- we'll talk about that in the exercises.)
We talked about this a bit when we were talking about compressing images, but let's review (since we're actually going to have to use this notation now. This is called RGB notation -- one of the least interesting acronyms you can find. It stands for Red-Green-Blue. The first pair of characters tells how much red is in the color, the second pair is for blue, and the last pair is for green. How do we interpret each pair? Easy! It's a two-digit hexadecimal number -- so every digit can be anything from 0 to F. So 00 means 0, and FF means 255. So the notation for red says "255 units of red (note that this is the maximum possible value), 0 units of blue, and 0 units of green." Not surprisingly, the result is red. The notations for blue and green work similarly. What about the color 000000? That means "no red, no blue, and no green" -- in other words, black. And "255 red, 255 green, 255 blue" -- all the color possible -- means white.
Here is little page that lets you experiment with this notation. Experiment with putting the same value in for all three colors (like 909090) -- what should happen? What it does it take to get some shade of yellow?
Here's a simple example. There's a very small mistake in this version of example 4 -- a missing quotation mark, which causes the first button not to work correctly. This kind of pickiness is a pain. Fortunately, there is one tool available that help you catch at least some of these mistakes. In your browser (Note! this is only guaranteed to work with Netscape Navigator!!), open a new page called javascript: (don't forget the colon). This opens a new window called the JavaScript console; in this window your browser will provide some information about JavaScript errors. When you're working on JavaScript assignments for this class, you'll probably want to keep the JavaScript console open all the time. (Another note: depending on how your browser was set up, it's possible that the javascript console will automatically appear every time there's an error; if this happens to you, you're in luck, as f ar as this class goes.)
So far, we haven't actually written very much JavaScript, but already there are a number of important rules to be aware of: