JavaScript
  Home arrow JavaScript arrow Page 3 - Introducing the Behaviour JavaScript Libra...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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

Introducing the Behaviour JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-03-19

    Table of Contents:
  • Introducing the Behaviour JavaScript Library
  • A basic example
  • Expanding the application range of the Behavior package
  • Applying rules to a different CSS selector

  • 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


    Introducing the Behaviour JavaScript Library - Expanding the application range of the Behavior package


    (Page 3 of 4 )

    In the section that you just read, I provided you with a clear example of how to use the Behaviour library to assign a simple "onclick" handler to one link, which was included into a sample web document. Therefore, let me show you how easy it is to apply the same handler to multiple web page links by utilizing the functionality offered by Behaviour.

    Now, suppose that you've created (because of laziness or ignorance) the following web document:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
      
    <title>Example of using multiple inline event
    handlers</title>
    <style type="text/css">
    h1{
      
    font: bold 24px Arial, Helvetica, sans-serif;
      
    color: #000;
    }
    a:link,a:visited{
      
    font: bold 12px Verdana, Arial, Helvetica, sans-serif;
      
    color: #00f;
      
    text-decoration: none;
    }
    a:hover{
      
    color: #f00;
      
    text-decoration: underline;
    }
    </style>
    </head>
    <body>
      
    <h1>Example of assigning multiple inline event handlers</h1>
      
    <p><a href="#" onclick="alert('This alert box has been badly
    generated by an inline event handler.')">This link element opens
    up an alert box using inline event handler.</a></p>
      
    <p><a href="#" onclick="alert('This alert box has been badly
    generated by an inline event handler.')">This link element opens
    up an alert box using inline event handler.</a></p>
      
    <p><a href="#" onclick="alert('This alert box has been badly
    generated by an inline event handler.')">This link element opens
    up an alert box using inline event handler.</a></p>
      
    <p><a href="#" onclick="alert('This alert box has been badly
    generated by an inline event handler.')">This link element opens
    up an alert box using inline event handler.</a></p>
      
    <p><a href="#" onclick="alert('This alert box has been badly
    generated by an inline event handler.')">This link element opens
    up an alert box using inline event handler.</a></p>
    </body>
    </html>

    As you can see, the previous web page includes a few annoying JavaScript inline handlers, which have been attached to a group of links. Indeed, this approach makes the whole source code really messy, but fortunately the problem can be quickly fixed by using the Behaviour package.

    As I did previously, I'm going to create the JavaScript file that assigns an "onclick" handler to all the links included into the respective web page. The file looks like this:

    var rulelink={
      
    'a' : function(element){
        
    element.onclick = function(){
          
    alert('This event handler has been assigned via the
    Behaviour library.');
         
    }
       
    }
    };
    Behaviour.register(rulelink);

    And here is the corresponding (X)HTML file that loads the JavaScript file I defined a few lines before:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
      
    <title>Example of assigning event handlers using Behavior
    JavaScript Library</title>
    <style type="text/css">
    h1{
      
    font: bold 24px Arial, Helvetica, sans-serif;
      
    color: #000;
    }
    a:link,a:visited{
      
    font: bold 12px Verdana, Arial, Helvetica, sans-serif;
      
    color: #00f;
      
    text-decoration: none;
    }
    a:hover{
      
    color: #f00;
      
    text-decoration: underline;
    }
    </style>
    <script language="javascript" src="behaviour.js"></script>
    <script language="javascript" src="rulelink.js"></script>
    </head>
    <body>
      
    <h1>Example of assigning event handler using Behavior
    JavaScript Library</h1>
      
    <p><a href="#">This link element opens up an alert box via
    Behavior JavaScript Library.</a></p>
      
    <p><a href="#">This link element opens up an alert box via
    Behavior JavaScript Library.</a></p>
      
    <p><a href="#">This link element opens up an alert box via
    Behavior JavaScript Library.</a></p>
      
    <p><a href="#">This link element opens up an alert box via
    Behavior JavaScript Library.</a></p>
      
    <p><a href="#">This link element opens up an alert box via
    Behavior JavaScript Library.</a></p>
    </body>
    </html>

    If you test the previous file on your browser, then you'll see that each of the links included into the web page will open up an alert box. This is similar to the erroneous example that I showed in the beginning of this section. However, in this case, both the structural markup and JavaScript code reside on different layers.

    All right, at this point you hopefully learned how to use the Behavior library to assign a simple "onclick" handler to multiple links coded on the same web page. Therefore, assuming that you want to see even more useful examples of how to take advantage of this excellent library, in the following section I'll show you how to use the package in conjunction with a different CSS selector.

    Want to learn how this will be achieved? Go ahead and read the next few lines.

    More JavaScript Articles
    More By Alejandro Gervasio


       · As you'll learn in this first article of the series, the Behaviour JavaScript...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







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