# Behaviour Javascript framework Contrib This contrib packages the third-party `Behaviour` Javascript event library, available from . Behaviour is suited to create javascript based interaction that degrades well when javascript is not available. Javascript file: [behaviour.js](http://www.dementia.org/twiki//view/TWiki/%TOPIC%/behaviour.js) (8.1K). The [compressed javascript file](http://www.dementia.org/twiki//view/TWiki/%TOPIC%/behaviour.compressed.js) (2.9K) has been processed by [ShrinkSafe](http://alex.dojotoolkit.org/shrinksafe/).
On this page:
## Introduction 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: > >
  • > > Click me to delete me > >
  • > > You can use: > > > > And then use css selectors to select that element and add javascript functions to it. > > var myrules = { > '#example li' : function(el){ > el.onclick = function(){ > this.parentNode.removeChild(this); > > } > } > }; > > Behaviour.register(myrules); ## Usage Include the javascript file: > 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: > Behaviour.register(myrules); ## Example If we have a 'normal' link to TWiki Web hometopic: [[TWiki Web Home|TWiki/WebHome]], 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. > [[%TWIKIWEB%.%HOMETOPIC%][TWiki Web Home]] > > > > The class name `link%TWIKIWEB%%HOMETOPIC%` will get expanded to `linkTWikiWebHome` Creates: [[TWiki Web Home|TWiki/WebHome]] ## Development - [Google Groups: Behaviour Javascript Library](http://groups.google.com/group/behaviour) - [Dean Edwards: Faster DOM Queries](http://dean.edwards.name/weblog/2006/03/faster/) - with a speed-up hack to Behaviour ## License Behaviour is freely distributable under the terms of an BSD license. For details, see the Behaviour website. ## Links - [Behaviour website](http://bennolan.com/behaviour/) - [Behaviour Google Group](http://groups.google.com/group/behaviour) ## Contrib Info
    Author: TWiki:Main.ArthurClemens
    Copyright: version 1.1 - Copyright (c) Ben Nolan and Simon Willison
    License: BSD
    Dependencies: None
    4 June 2006 1.000 First Version. Included Behaviour version: 1.1
    Home: http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%
    Feedback: http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev
    Appraisal: http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal
    **_Related Topics:_** [[TWikiPreferences]]