JavaScript
  Home arrow JavaScript arrow Page 2 - Understanding The JavaScript Event Mod...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Understanding The JavaScript Event Model (part 2)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 29
    2002-07-10

    Table of Contents:
  • Understanding The JavaScript Event Model (part 2)
  • Back To Basics
  • How's The Weather Up There?
  • X Marks The Spot
  • Enter The Stalker
  • Of Keys And Clicks
  • A Few Modifications
  • Tonight's Menu
  • Reducing The Crime Rate
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Understanding The JavaScript Event Model (part 2) - Back To Basics
    (Page 2 of 10 )

    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()">       
    ...
    </body>
    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
    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.

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


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway