%META:TOPICINFO{author="TWikiContributor" date="1530834156" format="1.1" version="10"}% ---+!! !Behaviour Javascript Framework Contrib
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:---++ Usage Include the Javascript file:You can use: Click me to delete me 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);
In your code you create a "rules" object, with sub-objects for each html element class name or id:
Apply the rules with: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 } } };
---+++ Example If we have a 'normal' link to TWiki Web hometopic: [[%SYSTEMWEB%.%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.Behaviour.register(myrules);
Creates: MOUSE OVER ME
---++ Development * [[http://groups.google.com/group/behaviour][Google Groups: Behaviour Javascript Library]] * [[http://www.nabble.com/Behaviour-Javascript-Library-f16264.html][Nabble - Behaviour Javascript Library forum & mailing list archive]] * [[http://groups.google.com/group/behaviour/browse_thread/thread/e9828f9fdb482ac1/8ca704730053e23f?#8ca704730053e23f][Behaviour2]] - update in the making, since 2006 ---++ License Behaviour is freely distributable under the terms of an BSD license. For details see the Behaviour website. ---++ Links * [[http://bennolan.com/behaviour/][Behaviour website]] * [[http://groups.google.com/group/behaviour][Behaviour Google Group]] ---++ Installation Instructions * For an __automated installation__, run the [[%SCRIPTURL{configure}%][configure]] script and follow "Find More Extensions" in the in the __Extensions__ section. * See the [[http://twiki.org/cgi-bin/view/Plugins/BuildContribInstallationSupplement][installation supplement]] on TWiki.org for more information. * Or, follow these __manual installation__ steps: * Download the ZIP file from the extension home on twiki.org (see below). * Unzip ==%TOPIC%.zip== in your twiki installation directory. * Set the ownership of the extracted directories and files to the webserver user. * Install the dependencies (if any). ---++ 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.%BR% TWiki distribution and updates/additions: © TWiki:Main/ArthurClemens.%BR% © 2006-2018 TWiki:TWiki/TWikiContributor | | License: | BSD for =behaviour.js= %BR% GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) for TWiki !BehaviourContrib | | Version: | 30438 (2018-07-16) | | Dependencies: | None | | Contrib Version: | 2018-07-05 | | Change History: | | | 2018-07-05: | TWikibug:Item7841: Copyright update to 2018 | | 2016-01-08: | TWikibug:Item7708: Copyright update to 2016 | | 2015-02-16: | TWikibug:Item7604: Switch from GPL v2 to v3 | | 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 http://groups.google.com/group/behaviour/browse_thread/thread/85137977bedf5ed/3cf3ba8065d41a8c#3cf3ba8065d41a8c][Raymond Irving]]. | | 02 Jul 2007 | 1.2 Integrated other faster code by Dean Edwards: [[http://dean.edwards.name/weblog/2006/06/again/][faster onload (again)]]. | | 08 Mar 2007 | 1.1 Integrated code by Dean Edwards (see [[#CodeUpdate][Code update version 1.1 with faster DOM queries]]). | | 04 Jun 2006 | 1.0 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:__ %SYSTEMWEB%.TWikiPreferences %META:FILEATTACHMENT{name="behaviour.js" attr="" comment="" date="1162075796" path="behaviour.compressed.js" size="2902" user="TWikiContributor" version="1"}%var myrules = { 'table.test td' : function(element) { element.onmouseover = function() { this.style.backgroundColor = highlightColor; return false; } element = null; // by setting this IE will not leak } }; Behaviour.register(myrules);