Prototype - Event Handling


Advertisements


Event management is one of the biggest challenges to achieve cross-browser scripting. Every browser has different approaches to handle key strokes.

Prototype Framework handles all cross browser compatibility issues and keeps you free from all trouble related to event management.

Prototype Framework provides Event namespace, which is replete with methods, that all take the current event object as an argument, and happily produce the information you're requesting, across all major browsers.

Event namespace also provides a standardized list of key codes you can use with keyboard-related events. The following constants are defined in the namespace −

S.No. Key Constant & Description
1.

KEY_BACKSPACE

Represent back space key.

2.

KEY_TAB

Represent tab key.

3.

KEY_RETURN

Represent return key.

4.

KEY_ESC

Represent esc key.

5.

KEY_LEFT

Represent left key.

6.

KEY_UP

Represent up key.

7.

KEY_RIGHT

Represent right key.

8.

KEY_DOWN

Represent down key.

9.

KEY_DELETE

Represent delete key.

10.

KEY_HOME

Represent home key.

11.

KEY_END

Represent end key.

12.

KEY_PAGEUP

Represent page up key.

13.

KEY_PAGEDOWN

Represent page down key.

How to Handle Events

Before we start, let us see an example of using an event method. This example shows how to capture the DOM element on which the event occurred.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         // Register event 'click' and associated call back.
         Event.observe(document, 'click', respondToClick);
  
         // Callback function to handle the event.
         function respondToClick(event) {
            var element = event.element();
            alert("Tag Name : " + element.tagName );
         }
      </script>
   </head>

   <body>
      <p id = "note"> Click on any part to see the result.</p>
      <p id = "para">This is paragraph</p>
      <div id = "division">This is divsion.</div>
   </body>
</html>

Output

Here is a complete list of all the methods related to Event. The functions you're most likely to use a lot are observe, element and stop.

Prototype Event Methods

NOTE − Make sure you at least have the version 1.6 of prototype.js.

S.No. Method & Description
1. element()

Returns the DOM element on which the event occurred.

2. extend()

Extends the event with all of the methods contained in Event.Methods.

3. findElement()

Returns the first DOM element with a given tag name, upwards from the one on which the event occurred.

4. isLeftClick()

Determines whether a button-related mouse event was about the "left" (primary, actually) button.

5. observe()

Registers an event handler on a DOM element.

6. pointerX()

Returns the absolute horizontal position for a mouse event.

7. pointerY()

Returns the absolute vertical position for a mouse event.

8. stop()

Stops the event's propagation and prevents its default action from being triggered eventually.

9. stopObserving()

Unregisters an event handler.

10. unloadCache()

Unregisters all event handlers registered through observe. Automatically wired for you. Not available since 1.6.



Advertisements