r4 - 24 Jun 2011 - 08:53 - AdminUser | r3 - 24 Jun 2011 - 08:53 - AdminUser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Behaviour Javascript framework Contrib |
Behaviour Javascript framework Contrib |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This contrib packages the third-party |
This contrib packages the third-party |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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). |
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). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Introduction |
Introduction |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From the website: |
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: |
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 |
Usage |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Include the javascript file: |
Include the javascript file: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In your code you create a "rules" object, with sub-objects for each html element class name or id: |
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 } } }; |
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: |
Or use nested identifiers: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var myrules = { '.menu li a' : function(element) { element.onclick = function() { // code here } } }; |
var myrules = { '.menu li a' : function(element) { element.onclick = function() { // code here } } }; |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Apply the rules with: |
Apply the rules with: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Behaviour.register(myrules); |
Behaviour.register(myrules); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example |
Example |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If we have a 'normal' link to a web hometopic: 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. |
If we have a 'normal' link to a web hometopic: 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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creates: |
Creates: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MOUSE OVER ME |
MOUSE OVER ME |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Leaking danger |
Leaking danger |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to |
Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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); |
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); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Development |
Development |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
License |
License |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Behaviour is freely distributable under the terms of an BSD license. For details see the Behaviour website. |
Behaviour is freely distributable under the terms of an BSD license. For details see the Behaviour website. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Links |
Links |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Installation Instructions |
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. |
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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Open configure, and open the "Extensions" section. Use "Find More Extensions" to get a list of available extensions. Select "Install". |
Open configure, and open the "Extensions" section. Use "Find More Extensions" to get a list of available extensions. Select "Install". |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If you have any problems, or if the extension isn't available in |
If you have any problems, or if the extension isn't available in |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contrib Settings |
Contrib Settings |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You can also set the global preference settings BEHAVIOURCONTRIB_DEBUG to 1 to make the contrib use uncompressed javascript sources, in the event of problems. |
You can also set the global preference settings BEHAVIOURCONTRIB_DEBUG to 1 to make the contrib use uncompressed javascript sources, in the event of problems. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contrib Info |
Contrib Info |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
r4 - 24 Jun 2011 - 08:53 - AdminUser | r3 - 24 Jun 2011 - 08:53 - AdminUser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||