- Prototype - Periodical Execution
- Prototype - Expressing Ranges
- Prototype - AJAX Support
- Prototype - JSON Support
- Prototype - Form Management
- Prototype - Event Handling
- Prototype - Enumerating
- Prototype - Templating
- Prototype - Basic Object
- Prototype - Hash processing
- Prototype - Array Processing
- Prototype - Strings Processing
- Prototype - Number Processing
- Prototype - Element Object
- Prototype - Utility Methods
- Prototype - Useful Features
- Prototype - Short Overview
- Prototype - Home
Prototype Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Prototype - Event Handpng
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 compatibipty 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 pst 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 cpck and associated call back. Event.observe(document, cpck , respondToCpck); // Callback function to handle the event. function respondToCpck(event) { var element = event.element(); alert("Tag Name : " + element.tagName ); } </script> </head> <body> <p id = "note"> Cpck on any part to see the result.</p> <p id = "para">This is paragraph</p> <span id = "spanision">This is spansion.</span> </body> </html>
Output
Here is a complete pst of all the methods related to Event. The functions you re most pkely 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. | Returns the DOM element on which the event occurred. |
2. | Extends the event with all of the methods contained in Event.Methods. |
3. | Returns the first DOM element with a given tag name, upwards from the one on which the event occurred. |
4. | Determines whether a button-related mouse event was about the "left" (primary, actually) button. |
5. | Registers an event handler on a DOM element. |
6. | Returns the absolute horizontal position for a mouse event. |
7. | Returns the absolute vertical position for a mouse event. |
8. | Stops the event s propagation and prevents its default action from being triggered eventually. |
9. | Unregisters an event handler. |
10. | Unregisters all event handlers registered through observe. Automatically wired for you. Not available since 1.6. |