Question:
What are events?
Answer:
Events are actions that ultimately result from user actions. For example, window.onload
occurs because the user typed your URL in the browser address bar,
or clicked a link to load a new page.
Question:
What are some specific events?
Answer:
Events include:
- window.onload
- window.onunload
- onclick
- onkeypress
- onmouseover
- onmouseout
- ...
A complete list and their descriptions may be found at:
w3schools JavaScript Event Reference
Question:
How do I manage events?
Answer:
Assign event handlers (functions) to the events of objects. For example:
document.getElementById("MyButton").onclick = BtnClick;
function BtnClick() {
alert("You click MyButton!");
}
Notice that when you assign the event handler, you do not use (),
otherwise that would be a function call instead of assigning the handler to the event.
In other words, the function would execute immediately, instead of waiting for the event
to cause the function to execute.
// do not use () when assigning an event handler to an object.
// the below code will not perform properly.
document.getElementById("MyButton").onclick = BtnClick();
Question:
How can I pass parameters to event handlers?
Answer:
That requires an advanced use of the function object.
Please review the source code for the Event Example in week 7.