Home arrow JavaScript arrow Page 4 - Introducing the Behaviour JavaScript Library
JAVASCRIPT

Introducing the Behaviour JavaScript Library


Separating the layers of your web pages makes them more compact and easier to maintain. The Behaviour JavaScript library makes it easy for even beginning coders to do this, and helps seasoned developers save time when building web sites. This article is the first in a three-part series that explains the features of this library.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
March 19, 2007
TABLE OF CONTENTS:
  1. · Introducing the Behaviour JavaScript Library
  2. · A basic example
  3. · Expanding the application range of the Behavior package
  4. · Applying rules to a different CSS selector

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials