Logo         Community
The Company
Your Account
Contact Us
Understanding The JavaScript Event Model (part 2)
Find out how the Event object can be used to manipulate the dimensions of a Web page and intercept keyboard and mouse events

| Back To Basics |

First up, a quick recap. The JavaScript event model provides a way for a user to interact with JavaScript. It consists of two basic components, events and event handlers.

An event may be defined, very simply, as an action performed on a Web page - for example, clicking a button, moving the mouse pointer over a hyperlink and so on.

An event handler, as the name suggests, handles an event - it defines the action to be taken by a script when a particular event (or type of event) occurs. Event handlers exist for most of the common events that are generated on a Web page, including mouse movement, mouse clicks, keyboard activity and page loads.

Here's a quick example that might make this theory a little clearer:

<body onLoad="thisFunction()">     

If you were to translate the line of code above into English, it would read, "invoke the JavaScript function thisFunction() when the page loads". The event handler in this case is the onLoad handler, which can be used to perform specific actions when a Web page loads into the browser.

JavaScript comes with a whole bunch of event handlers - here's a brief list:

onClick - invoked when the user clicks the specified object

onMouseOver - invoked when the user passes the mouse over the target object

onMouseOut - invoked when the mouse pointer leaves the target object

onSubmit - invoked when the user clicks the Submit button in a form

onChange - invoked when the user changes the contents of a text field

onSelect - invoked when the user selects the contents of a text field

onReset - invoked when the user clicks the Reset button in a form

onLoad - invoked when the target image or document is loaded

onUnload - invoked when the target image or document is unloaded

Whenever an event takes place, JavaScript creates an Event object. This Event object, like all objects, possesses certain properties, which provide additional information about the event generated. So, for example, if you hit a key on your keyboard, JavaScript would create an Event object containing information on the key pressed, the ASCII code, and any additional keys that were pressed in combination with it. Or, if you clicked a mouse button, JavaScript would spawn an Event object containing information on which button was clicked, and where the mouse pointer was at the moment of click.

With all this information at your disposal, it's fairly easy to write client-side scripts that take advantage of it to do new and cool things on your Web page. I'll be showing you a few shortly - but first, take a look at a brief sampling of the properties that a typical Event object exposes:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Property        Description (compatibility)

type              returns a string value indicating the event type

x                  returns the horizontal position of the cursor relative to the object

y                  returns the vertical position of the cursor relative to the object

height            returns the height of the object (NN)

width              returns the width of the object (NN)

modifiers          returns the details of any modifier keys that were held down during a key or mouse event

which              returns integer value indicating which mouse button or key was pressed (NN)

keyCode            returns Unicode value of the key pressed (IE)

button            returns integer value of the mouse button pressed (IE)

Intrigued? Let's do something useful, then.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback