JavaScript
  Home arrow JavaScript arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 - Applying rules to a different CSS selector


    (Page 4 of 4 )

    In the previous section I demonstrated how to use the Behaviour package to attach the same function to all the links included in a sample web document. It was, admittedly, a straightforward and painless process.

    Now I'd like you to pay attention to the following example.  It illustrates how to assign the same JavaScript function to a group of paragraphs. This shows off the flexibility provided by this excellent JavaScript package when it comes to modifying CSS selectors.

    That being said, the example in question is as follows:

    (definition for "rulespar.js" file)

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

    <!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>Testing Behaviour JavaScript Library</title>
    <style type="text/css">

    h1{
      
    font: bold 24px Arial, Helvetica, sans-serif;
      
    color: #00f;
    }
    p{
      
    font: bold 12px Verdana, Arial, Helvetica, sans-serif;
      
    color: #000;
    }
    </style>
    <script language="javascript" src="behaviour.js"></script>
    <script language="javascript" src="rulespar.js"></script>
    </head>
    <body>
      
    <h1>Testing Behaviour JavaScript Library</h1>
      
    <div>
        
    <p>This first paragraph will be attached to an onclick
    event handler.</p>
        
    <p>This second paragraph will be attached to an onclick
    event handler.</p>
        
    <p>This third paragraph will be attached to an onclick
    event handler.</p>
        
    <p>This fourth paragraph will be attached to an onclick
    event handler.</p>
      
    </div>
    </body>
    </html>

    In this particular case, I followed the same approach shown with the previous examples. First, I defined the corresponding JavaScript file, which is responsible for attaching a specific function to a predefined CSS selector (in this case, a few paragraphs), and then created the (X)HTML file that includes the aforementioned JavaScript code.

    If you're curious about how the previous example works, try it on your own browser and you'll see how an alert box is neatly displayed each time you click on a paragraph.

    Wrapping up

    That's all for the moment. In this first article of the series I showed you some basic yet instructive examples of how to use the functionality provided by the Behaviour library. In particular, I showed you how it could help you create clean and unobtrusive JavaScript code for working with specific CSS selectors.

    However, this is merely the beginning of this journey. In the next part of the series I'll show you how to use the Behaviour package to work with different CSS selectors, such as IDs and classes. You won't want to miss it.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - 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






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