Skip to main content

BehaviorS.js - An alternative to Behaviour.js, event:Selectors and Low Pro libs for unobtrusive JavaScript programming

BehaviorS.js yet another unobtrusive JavaScript library similar to Behaviour.js and event:Selectors but in implementation uses hash based lookup without extending elements; so presumably it should be faster than the rest. The original script and idea was by JLof; I extended it for DOMContentLoaded support, optimized a bit to avoid scanning of more depths, and added new rules support.

I wanted to document the plug a long time and just got time to do it.

For the time being BehaviorS.js is available here

Update (2006-09-11): Coralized the link to BehaviorS.js so as to save the load on free brinkster.com webpage

Update (2006-09-27): If the coralized link to BehaviorS.js doesn't work, use http://www21.brinkster.com/guideme/BehaviorS/

Update (2025-06-07): Now available in https://github.com/rrjanbiah/behaviorsjs

Comments

Anonymous said…
A bit different than the other approaches but so far so good.

I like your more abbreviated approach. I really don't need super complex filtering for elements or to dig deep into the DOM to attach events.

I always felt like the other approaches might be a bit overcomplex in their ability to handle some pretty rare scenarios. The flexibility seemed to have a performance impact and I like how you can adjust your flexibility with a corresponding impact on performance.

I need to do some more testing but, again, so far so good.
Anonymous: Thanks for your kind words. All the credits go to JLof for sharing his wonderful piece of code.
Anonymous said…
Hi,

Very nice library. I have just started looking at it and I see where
it's possible to do a lot of things. I'm not sure if it will be able to
handle onblur and onfocus events but it's a great start.


Does the lib handle things like onchange on keypress? Can I do things
like "#myid ul li:click" selections?
Anonymous:

Yes, onblur and onfocus events capturing is possible.

Selection such as "#myid ul li:click" isn't possible. It should rather be "#myid:click" or "LI:click" (tag should be in upper case) or "UL:click"

But, it's possible to extend your own syntax by touching the source.
Anonymous said…
Thanks Rajesh!

As soon as I get sometime I'll check out the source and see what I can do.

This is truely a great library.
Anonymous:

Thanks for your kind words. If you improve it, kindly inform me so that the improvements can be incorporated. Thanks.
Anonymous said…
The links don't work.
Anonymous: The link is coralized so as to avoid the load on the free webpage (BehaviorS.js page gets quite traffic lately).

If that coralized link doesn't work, you may have to use the direct link that I updated under "Update (2006-09-27)".
Anonymous said…
Very fast, but a couple of problems:

- Can't stop an event handler's default behavior; preventDefault() won't work in IE (of course) and returning false doesn't seem to work

- IE doesn't seem to be picking up onsubmit behaviors

So far, so good, but those limitations make it unworkable for me right now.
Dan:

1. You may need to use cross-browser function to stop event propagation. Here is the one from Prototype:

function stopEvent(event) {
if (event.preventDefault) {
event.preventDefault();
event.stopPropagation();
} else {
event.returnValue = false;
event.cancelBubble = true;
}
}

2. Just noted that IE doesn't support global (document.body.attachEvent("onsubmit",...)) event handler for forms. Thanks for reporting it. I do have a patch now (locally); but still googling to see if there is any better out there. If you have any, kindly ping me. Thanks.
Dan:

This is the update to my previous reply.

IE seems to require attaching events to particular element so as the onsubmit, onblur, onfocus (and any other untried events) events will work.

The only patch I tried is extending elements individually--which is obviously defeating the purpose of this library; and hence gave up that patch. And, I'm still thinking of better way to handle this.

By the way, for the onsubmit, IE seems to work on 'FORM:click'.
Anonymous said…
Good library, good documentation - thanks! Faced errors in digDepth in IE with className is null error. Worked around it with some checks in the handle function. Also, a majority of selectors would probably apply, in the web 2.0 era, to specific elements by id instead of classname or type.


for (var i = 0, found = false;
!found && nd && i < BehaviorS.digDepth;
++i, nd = nd.parentNode) {
found = BehaviorS.invoke("#" + nd.id + ":" + e.type, nd, e) || found;
if (!found) {
found = BehaviorS.invoke(nd.nodeName + ":" + e.type, nd, e) || found;
}
if (!found && nd.className != null) {
Anonymous:

Thanks for the kind words and the patch. I'm still looking how to reproduce the bug you're reporting. BTW, if you could tell me your real name, I may credit the patch to your name.

I have also planned to do few minor improvements by next weekend.

(Apologies for the late reply; been on vacation and then got busy for sometime)
Anonymous said…
Hi,

I came across your lib while browsing the behaviour.js list for solutions to performance problems when loading large pages. First tests look very promising (from my 'JS lib user' perspective). With your stopEvent patch, it seems to do all I want, in no time!

Any notes on browser + platform compatibility? So far, behaviorS seems to work from IE5 and Opera7 onwards, and recent FF, all on Windows XP. Are there any figures for other browsers (Mac?).

Kind regards,

Ron Van den Branden
Ron Van den Branden:

Thanks for your kind words. But, I must confess that I haven't tested in many platforms and I hope it will work--thanks for your information on Opera.

I tested in FF1.5, it works for all events. Also tested in IE6, it works for many events except blur, focus and submit (Please check my reply above.)
Eugene said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.

Popular posts from this blog

Storing unicode texts in MySQL with phpMyAdmin

Today, I've received a personal mail/request from Sivanantham Hemamalini, working for IT leisure in Singapore. Since I was in company when received the mail, I couldn't answer immediately. If I understand the question right, it is about inputting Unicode texts especially Tamil in phpMyAdmin. PhpMyAdmin 's default characterset is iso-8859-1 and so if we enter anything in the form, browser will convert it into numerical html entities. Say for example, if we enter தமிழ் and submit the form, it will convert it to & #2980;& #2990;& #3007;& #2996;& #3021; . Because of this browser's behavior, it will be difficult to store the Unicode text as it is. Solutions Immediate solution I could think of is changing or forcing the browser's character encoding into utf-8. In Mozilla Firefox, it can be set via View -> Character Encoding -> Unicode (UTF-8) Another elegant solution might be changing the phpMyAdmin configurations so that it send...

Who is S.P. Udayakumar, (Koodankulam anti-nuclear) activist of the year

I greatly admired S.P. Udayakumar  ( Koodankulam anti-nuclear activist) for his calm and composed response; it is usually not easy to reply moronic Narayanasamy and moronic Ponraj (advisor of Abdul Kalam) who're always blabbering irrationally. By wrongly judging from his sober and humble look, I'd thought that he's an illiterate villager from Koodankulam. But, recently I came to know that he's studied in USA and worked as a professor there. My respect for him greatly increased when I compared him with other exhibitionist NRIs. Here are the details that I collected... Name: S.P. Udayakumar Age: 50 Native: Nagercoil Family: Wife Meera running SACCER primary and middle school, 2-sons Education M.A. (Peace Studies) from Notre Dame University in the United States (1990) Ph.D. (Political Science) from the University of Hawaii (1996) (Past) Occupation  Research fellow at Kirwan Institute Professor at Governor's School of Public Issues and the Fut...

Amazon India Prime Offer - A scam or a bug?

TLDR; Amazon.in shows and sends me a Prime offer that I can't avail (it leads to an error message) I have escalated this issue to Amazon India through various means, but the team wants screenshots (even after sharing them through Twitter) and so here's the post explaining that... 1. Amazon India sends me an email: Your Prime membership ends soon - Renew early to lock in old price of ₹999 1499 for another year of Prime. The newsletter comes like this: Rajesh, Renew Prime early and save ₹500 Your Prime membership ends soon Renew today Expiring soon DON'T GO A DAY WITHOUT PRIME! Your Prime membership ends soon - Renew early to lock in old price of ₹999 1499 for another year of Prime. Continue enjoying uninterrupted Prime benefits. Renew today Kindly ignore if you have already availed this offer. Please note that pro...