Java Reference
In-Depth Information
some text all cause events to occur. Another example, which is used quite frequently, is the load
event for the page: The window raises (or fires) a notification when the page is completely loaded
in the browser.
Why should you be interested in events?
Take as an example the situation in which you want to make a menu pop up when the user clicks
anywhere in your web page. Assuming that you can write a function that will make the pop‐up
menu appear, how do you know when to make it appear, or in other words, when to call the
function? You somehow need to intercept the event of the user clicking in the document, and make
sure your function is called when that event occurs.
To do this, you need to use something called an event handler , or listener . You associate this with the
code that you want to execute when the event occurs. This provides you with a way of intercepting
events and making your code execute when they have occurred. You will find that adding an event
handler to your code is often known as “connecting your code to the event.” It's a bit like setting an
alarm clock—you set the clock to make a ringing noise when a certain event happens. With alarm
clocks, the event is when a certain time is reached.
tYpes of events
Web development, especially when it comes to JavaScript, is primarily event‐driven , meaning that the
flow of the program is controlled by events. In other words, a large portion of your JavaScript code
usually only executes when an event occurs, and you can listen for many events.
Take a moment and think about how you interact with a web page. On a computer or laptop, you
move your mouse around the page, perhaps you select text that you want to copy and paste into
your note‐taking program, and you definitely click things (like links). On touch‐based devices,
you tap items in the page. And on all web‐enabled devices, you fill out forms by typing keys on the
keyboard. Virtually everything you do triggers an event, and a lot of the time, you want to write
code that reacts to some of those events.
Following is a list of the many types of events that you can listen for and react to:
Mouse events: These occur when the user does something with the mouse, such as moving
the cursor, clicking, double‐clicking, dragging, and so on.
Keyboard events: These occur when keys on the keyboard are pressed or depressed. Though
commonly used in conjunction with forms, keyboard events occur every time the user presses
or depresses a key.
Progression events: These are more generic events that occur at different stages of an object.
For example, when the document loads.
Form events: These occur when something in the form changes.
Mutation events: These occur when DOM nodes are modified.
Touch events: These occur when the user touches the sensor.
Error events: These occur when an error occurs.
 
Search WWH ::




Custom Search