BehaviourCategory: Programming, Web Design | Leave a Comment |
4 09 2005 |
Behaviour is a new javascript library that I’ve been using lately. It really compliments unobtrusive javascript. It allows you to define rules that map css style selectors to javascript functions. Here is an example of using a hyperlink class to define a common functionality for all links on a page that contain that class.
window.onload = function () {
Behaviour.register(defineRules());
Behaviour.apply();
}
function defineRules() {
return {
‘a.popup’ : function (element) {
var popupLocation = element.href;
element.onclick = function () {
window.open(popupLocation);
}
element.href = ‘javascript:void(0);’;
}
}
}
This will look at the page and every hyperlink with the class popup with be converted to a javascript popup using the href value. It is really nice because degrades nicely when javascript is disabled.