Home arrow JavaScript arrow Page 2 - Understanding The JavaScript Event Model (part 2)

Back To Basics - JavaScript

In this concluding article on the JavaScript event model, findout how the Event object can be used to do ever more complex things,including manipulating the dimensions of a Web page and tracking andintercepting keyboard and mouse events.

  1. Understanding The JavaScript Event Model (part 2)
  2. Back To Basics
  3. How's The Weather Up There?
  4. X Marks The Spot
  5. Enter The Stalker
  6. Of Keys And Clicks
  7. A Few Modifications
  8. Tonight's Menu
  9. Reducing The Crime Rate
  10. Endgame
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 32
July 10, 2002

print this article


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:
Property        Description (compatibility)
type               returns a string value indicating the event type 
x                  returns the horizontal position of the cursor
to the object 
y                  returns the vertical position of the cursor relative
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
down during a key or mouse event
which              returns integer value indicating which mouse button
key was pressed (NN)
keyCode            returns Unicode value of the key pressed (IE)
button             returns integer value of the mouse button pressed
Intrigued? Let's do something useful, then.

>>> More JavaScript Articles          >>> More By Team Melonfire, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates


Dev Shed Tutorial Topics: