You are here: TWiki> TWiki Web>BehaviourContrib (2010-05-16, TWikiContributor)
create new tag
, view all tags

Behaviour Javascript Framework Contrib

Behaviour Javascript event library to create Javascript based interactions that degrade well when Javascript is not available


This contrib packages the third-party Behaviour Javascript event library, available from

Behaviour uses CSS selectors to subscribe to Javascript event handlers. This allows to create clean code, separated from HTML (and well suited to create Javascript based interaction that degrades nicely when Javascript is not available).

From the website:

After all the work of WASP and others to promote clean markup, valid pages and graceful degradation via css - it sucks that we're going back to tag soup days by throwing javascript tags into our html.

The better way to do javascript is to do it unobtrusively. PPK and Simon Willison have been recommending this approach for ages. And it's definitely the way to go. The only problem is that it's a bit of a pain in the ass.

That's why I came up with Behaviour - my solution to unobtrusive javascript behaviours.

How does it work?

Behaviour lets you use CSS selectors to specify elements to add javascript events to. This means that instead of writing:

 <a onclick="this.parentNode.removeChild(this)" href="#">
  Click me to delete me

You can use:

<ul id="example">
  <a href="/someurl">Click me to delete me</a>

And then use css selectors to select that element and add javascript functions to it.

var myrules = {
 '#example li' : function(el){
  el.onclick = function(){


Include the Javascript file:

<script type="text/javascript" src="%PUBURL%/%SYSTEMWEB%/BehaviourContrib/behaviour.js"></script>

In your code you create a "rules" object, with sub-objects for each html element class name or id:

var myrules = {
 '.classname' : function(element) {
  // element event
  element.onclick = function() {
   // code here
 '#id' : function(element) {
  // element event
  element.onclick = function() {
   // code here

Or use nested identifiers:

var myrules = {
 '.menu li a' : function(element) {
  element.onclick = function() {
   // code here

Apply the rules with:



If we have a 'normal' link to TWiki Web hometopic: TWiki Web Home, we can use javascript to make it open a popup window. When javascript is not available the link behaviour defaults to opening the page in the current window.

<div id="demoblock" style="padding:1em; width:100px; text-align:center;">

<script type="text/javascript">
// <![CDATA[
var myrules = {
 '#demoblock' : function(el) {
  var defaultColor = '#A3D6F8';
  var highlightColor = '#4A7FB5'; = defaultColor;

  el.onmouseover = function() { = highlightColor;
   return false;
  el.onmouseout = function() { = defaultColor;
   return false;
 '#demoblock span' : function(el) {

  var text = el.innerHTML;

  var fisherYates = function (inArray) {
   var i = inArray.length;
   if ( i == 0 ) return false;
   while ( --i ) {
    var j = Math.floor( Math.random() * ( i + 1 ) );
    var tempi = inArray[i];
    var tempj = inArray[j];
    inArray[i] = tempj;
    inArray[j] = tempi;

  var randomize = function(inText) {
   var letters = inText.split('');
   return letters.join('');
  el.onmouseover = function() {
   this.innerHTML = randomize(text);
   return false;
  el.onmouseout = function() {
   this.innerHTML = text;
   return false;
// ]]>



Leaking Danger

Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to null:

var myrules = {
 'table.test td' : function(element) {
  element.onmouseover = function() { = highlightColor;
   return false;
  element = null; // by setting this IE will not leak  



Behaviour is freely distributable under the terms of an BSD license. For details see the Behaviour website.


Installation Instructions

You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server where TWiki is running.

Like many other TWiki extensions, this module is shipped with a fully automatic installer script written using the BuildContrib.

  • If you have TWiki 4.2 or later, you can install from the configure interface (Go to Plugins->Find More Extensions)
  • If you have any problems, then you can still install manually from the command-line:
    1. Download one of the .zip or .tgz archives
    2. Unpack the archive in the root directory of your TWiki installation.
    3. Run the installer script ( perl <module>_installer )
    4. Run configure and enable the module, if it is a plugin.
    5. Repeat for any missing dependencies.
  • If you are still having problems, then instead of running the installer script:
    1. Make sure that the file permissions allow the webserver user to access all files.
    2. Check in any installed files that have existing ,v files in your existing install (take care not to lock the files when you check in)
    3. Manually edit LocalSite.cfg to set any configuration variables.

Contrib Settings

  • Set SHORTDESCRIPTION = Behaviour Javascript event library to create Javascript based interactions that degrade well when Javascript is not available

You can also set the global TWiki variable BEHAVIOURCONTRIB_DEBUG to 1 to make the contrib use uncompressed javascript sources, in the event of problems.

Contrib Info

Author: TWiki:Main/ArthurClemens
Copyright: Code: behaviour.js version 1.1 - Copyright (c) Ben Nolan and Simon Willison.
TWiki distribution and updates/additions: © TWiki:Main/ArthurClemens.
© 2006-2010 TWiki:TWiki/TWikiContributor
License: BSD for behaviour.js
GPL (GNU General Public License External link mark) for TWiki BehaviourContrib
Version: 18694 (2010-05-29)
Dependencies: None
Contrib Version: 1.4
Change History:  
2010-05-15: TWikibug:Item6433 - doc improvements; replacing TWIKIWEB with SYSTEMWEB
17 Oct 2007 1.3 Replaced "faster code" by other code from Dean Edwards, [[ packed by][Raymond Irving]].
02 Jul 2007 1.2 Integrated other faster code by Dean Edwards: faster onload (again) External link mark.
08 Mar 2007 1.1 Integrated code by Dean Edwards (see Code update version 1.1 with faster DOM queries).
04 Jun 2006 1.0 First Version. Included Behaviour version: 1.1.

Related Topics: TWikiPreferences

Topic attachments
I Attachment Action Size Date Who Comment
JavaScriptjs behaviour.js manage 2.8 K 2006-10-28 - 22:49 TWikiContributor  
Edit | Attach | PDF version | Print version |  | Raw View | Raw edit | Backlinks | History: r8 < r7 < r6 < r5 < r4 | More topic actions
Topic revision: r8 - 2010-05-16 - TWikiContributor

This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 1999-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Note: Please contribute updates to this topic on at TWiki:TWiki.BehaviourContrib.