JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - A basic example


    (Page 2 of 4 )

    To demonstrate how the Behaviour library can be used to remove completely ugly inline JavaScript event handlers from the structural markup of a given web document, let me show you a couple of basic yet educational examples.

    Please take a look at the first example. It shows how to attach an alert box to a simple paragraph element via an inline "onclick" event handler. The code sample is as follows:

    <!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 inline event handler</title>
    </head>
    <body>
       <h1>Example of inline event handler</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</a></p>
    </body>
    </html>

    Pretty bad, right? This approach should always be avoided when coding your web documents! Now that you have seen the incorrect way to assign the previous event handler, let me show you how to use the Behaviour package to recreate the same example with clean, unobtrusive JavaScript code.

    First I'll create the following independent JavaScript file. In this file I'll configure which CSS selectors will be attached to the inline "onclick" handler that you learned before. The signature of this file is as follows:

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

    As shown above, I first defined an associative array, in this case called "rulelink." The key of its unique element is the CSS selector that will be used with the event handler, and the value consists of a function that will be called when the respective event is triggered.

    Finally, the rule in question is applied to the selector via the handy "register()" method. Quite simple, isn't it?

    All right, having defined the previous JavaScript file, all I have to do here is include it into the corresponding web document, as indicated below:

    <!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 handler 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>
    </body>
    </html>

    As you can see, the previous (X)HTML file first loads the source file that corresponds to the Behaviour library, and then includes the JavaScript file that I created a few lines above to assign an "onclick" event handler to the link present in the web page.

    Hopefully, after seeing how the Behaviour package can be used to assign a specific function to a predefined CSS selector, you'll realize the potential offered by this library. It's possible to quickly create web pages that completely separate their structural layer from the respective behavior. That's coding unobtrusive JavaScript indeed!

    Okay, at this point I'm pretty sure that you learned the methodology required for using the Behaviour library. So let's move forward and continue learning more practical examples that will illustrate how to get the most out of this useful package.

    To see how these examples will be developed, click on the link below and keep reading.

    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 1 hosted by Hostway