Saturday, April 30, 2011

JavaScript Events - X


In the previous lesson, we used an event handler to trigger off a call to our function. There are 18 event handlers that you can use to link your HTML elements to a piece of JavaScript.
When you write a JavaScript function, you will need to determine when it will run. Often, this will be when a user does something like click or hover over something, submit a form, double clicks on something etc.
These are examples of events.
Using JavaScript, you can respond to an event using event handlers. You can attach an event handler to the HTML element for which you want to respond to when a specific event occurs.
For example, you could attach JavaScript's onMouseover event handler to a button and specify some JavaScript to run whenever this event occurs against that button.

The HTML 4 specification refers to these as intrinsic events and defines 18 as listed below:
Event HandlerEvent that it handles
onBlurUser has left the focus of the object. For example, they clicked away from a text field that was previously selected.
onChangeUser has changed the object, then attempts to leave that field (i.e. clicks elsewhere).
onClickUser clicked on the object.
onDblClickUser clicked twice on the object.
onFocusUser brought the focus to the object (i.e. clicked on it/tabbed to it)
onKeydownA key was pressed over an element.
onKeyupA key was released over an element.
onKeypressA key was pressed over an element then released.
onLoadThe object has loaded.
onMousedownThe cursor moved over the object and mouse/pointing device was pressed down.
onMouseupThe mouse/pointing device was released after being pressed down.
onMouseoverThe cursor moved over the object (i.e. user hovers the mouse over the object).
onMousemoveThe cursor moved while hovering over an object.
onMouseoutThe cursor moved off the object
onResetUser has reset a form.
onSelectUser selected some or all of the contents of the object. For example, the user selected some text within a text field.
onSubmitUser submitted a form.
onUnloadUser left the window (i.e. user closes the browser window).
The events in the above table provide you with many opportunities to trigger some JavaScript from within your HTML code.
I encourage you to bookmark this page as a reference - later on you may need a reminder of which events you can use when solving a particular coding issue.

No comments:

Post a Comment