PhpRiot
Follow phpriot on Twitter
Sponsored Link
Download Article
Download this article or the entire “Eight Weeks of Prototype” series with all listings and files.




More information
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

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