1 # <a name="Behaviour Javascript framework C"></a><a name=" Behaviour Javascript framework "></a> Behaviour Javascript framework Contrib
3 This contrib packages the third-party `Behaviour` Javascript event library, available from <http://bennolan.com/behaviour/>.
5 Behaviour is suited to create javascript based interaction that degrades well when javascript is not available.
7 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/).
9 <div><span>On this page:</span><ul>
10 <li><a href="#Introduction"> Introduction</a></li>
11 <li><a href="#Usage"> Usage</a></li>
12 <li><a href="#Example"> Example</a></li>
13 <li><a href="#Development"> Development</a></li>
14 <li><a href="#License"> License</a></li>
15 <li><a href="#Links"> Links</a></li>
16 <li><a href="#Contrib Info"> Contrib Info</a></li>
20 ## <a name="Introduction"></a> Introduction
24 > 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.
26 > 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.
28 > That's why I came up with Behaviour - my solution to unobtrusive javascript behaviours.
30 > **How does it work?**
32 > Behaviour lets you use CSS selectors to specify elements to add javascript events to. This means that instead of writing:
35 > <a onclick="this.parentNode.removeChild(this)" href="#">
36 > Click me to delete me
44 > <a href="/someurl">Click me to delete me</a>
48 > And then use css selectors to select that element and add javascript functions to it.
51 > '#example li' : function(el){
52 > el.onclick = function(){
53 > this.parentNode.removeChild(this);
59 > Behaviour.register(myrules);
61 ## <a name="Usage"></a> Usage
63 Include the javascript file:
65 > <script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.compressed.js"></script>
67 In your code you create a "rules" object, with sub-objects for each html element class name or id:
70 > '.classname' : function(element) {
72 > element.onclick = function() {
77 > '#id' : function(element) {
79 > element.onclick = function() {
85 > Or use nested identifiers:
88 > '.menu li a' : function(element) {
89 > element.onclick = function() {
97 > Behaviour.register(myrules);
99 ## <a name="Example"></a> Example
101 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.
103 > <span class="link%TWIKIWEB%%HOMETOPIC%">[[%TWIKIWEB%.%HOMETOPIC%][TWiki Web Home]]</span>
105 > <script type="text/javascript">
108 > '.link%TWIKIWEB%%HOMETOPIC% a' : function(el){
109 > el.onclick = function() {
110 > // open in a popup with no other attributes than template 'viewplain'
111 > launchTheWindow(this.href,null,null,null,"viewplain");
117 > Behaviour.register(myrules);
121 > The class name `link%TWIKIWEB%%HOMETOPIC%` will get expanded to `linkTWikiWebHome`
125 <span>[[TWiki Web Home|TWiki/WebHome]]</span>
127 ## <a name="Development"></a> Development
129 - [Google Groups: Behaviour Javascript Library](http://groups.google.com/group/behaviour)
130 - [Dean Edwards: Faster DOM Queries](http://dean.edwards.name/weblog/2006/03/faster/) - with a speed-up hack to Behaviour
132 ## <a name="License"></a> License
134 Behaviour is freely distributable under the terms of an BSD license. For details, see the Behaviour website.
136 ## <a name="Links"></a> Links
138 - [Behaviour website](http://bennolan.com/behaviour/)
139 - [Behaviour Google Group](http://groups.google.com/group/behaviour)
141 ## <a name="Contrib Info"></a> Contrib Info
143 <table border="1" cellpadding="0" cellspacing="0">
146 <td> TWiki:Main.ArthurClemens </td>
149 <td> Copyright: </td>
150 <td> version 1.1 - Copyright (c) Ben Nolan and Simon Willison </td>
157 <td> Dependencies: </td>
161 <td> 4 June 2006 </td>
162 <td> 1.000 First Version. Included Behaviour version: 1.1 </td>
165 <td align="right"> Home: </td>
166 <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%</a></td>
169 <td align="right"> Feedback: </td>
170 <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev</a></td>
173 <td align="right"> Appraisal: </td>
174 <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal</a></td>
178 **_Related Topics:_** [[TWikiPreferences]]