Introduction
Inline javascripts tend to be hard to maintain in the clutter of code that forms when web-developers and web-designers have to work in the same documents.
The classBehaviour script was written in an effort to separate the markup of a page from the scripting and thus eliminate inline scripting altogether.
ClassBehaviour effects can be implemented without writing a single line of code.
The Idea Behind It
ClassBehaviour is my method for the rapid development of accessible yet dynamic browser-based user-interfaces.
Not a Framework
ClassBehaviour is not yet another javascript framework. I abhor the needless flood of "frameworks" trying to make javascript "easier" by adding another layer of abstraction. Instead, what I call, "classbehaviours" are simple scripts which are easily applied through the class-names in an HTML document.
What It Does
To apply event-handlers to html-elements, classnames are used. The classBehaviour script will scan every html-element in the page for certain keywords in the classnames and apply event-handlers accordingly. The "classbehaviours.js" contains the main parsing section of the script, while the "classBehaviours.js" contains all the seperate behaviours required on the page. These seperate functions can be removed and added freely to avoid the lengthy download of the complete collection.
The example below explains how a simple dhtml effect would be implemented:
There are blinking words in this sentence.
The script works best if it doesn't have to wait for the whole document including all graphics and plugins to be loaded. Therefor the optimal place is near the bottom of the documents with the "classbehaviours.js" preceding "classbehaviours.js".
The example below is also a valid way to include the scripts, however the parser will rely on the "onload" event to start the effects. This may introduce a noticeable delay in pages rich in media.
How It Works
Inside the classBehaviour include the following function scans all the tags for keywords.
In this case it applies the "blink" function to tag it finds. This function alternates the element between two classnames defined in the stylesheet to make it blink.
Conclusion
The example above demonstrated how much inline javascript clutter is avoided using this method. The DHTML behaviour can easily be applied and degrades gracefully: Should javascript or stylesheets be disabled or overridden there should be no impact on the usability of the content.
Most examples on www.classbehaviours.com have been developed using this principle.
Comments on this Article