PhpRiot
Download This Article
Download this article or the entire “Eight Weeks of Prototype” series with all listings and files.




More information
Related Books
JavaScript: The Good Parts

JavaScript: The Good Parts

Most programming languages contain good and bad parts, but JavaScript has more than its share of...

Practical Prototype and script.aculo.us (Expert's Voice in Web Development)

Practical Prototype and script.aculo.us (Expert's Voice in Web Development)

As a JavaScript developer, you will already be aware of some of the time saving, convenience,...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Eight Weeks of Prototype: Week 4, Event Handling in Prototype

Checking for Stopped Events

As mentioned in the previous section, if you have multiple event handlers, each of them will still be executed even if you called Event.stop() at any time. You can, however, check to see if an earlier handler has already stopped the event by reading the stopped property.

Listing 8 shows an example of doing this. In it, I have defined two different handlers for the click event, each of which will display an alert box. However, the alert box will only be shown if the event hasn't already been stopped.

Listing 8 Checking for stopped events (listing-8.html)
<html>
    <head>
        <title>Checking for stopped events</title>
        <script type="text/javascript" src="/js/prototype.js"></script>
    </head>
    <body>
        <div id="foo">
            Click here
        </div>
 
        <script type="text/javascript">
            function handler1(e)
            {
                if (!e.stopped) {
                    alert('Handler 1');
                    Event.stop(e);
                }
            }
 
            function handler2(e)
            {
                if (!e.stopped) {
                    alert('Handler 2');
                    Event.stop(e);
                }
            }
 
            $('foo').observe('click', handler1);
            $('foo').observe('click', handler2);
        </script>
    </body>
</html>

In This Article


Tagged in ,