[thelist] javascript default event handlers, default events. [TIP]

marcus m-lists at bristav.se
Thu Sep 1 04:10:58 CDT 2005


Kowalkowski, Lee (ASPIRE) wrote:
> DHTML uses event handlers functions to invoke behaviour.  

Yes, but an event handler is still an event handler if you attach it via 
DOM or through markup.

> If we can see the
> invocations in the HTML source it's easier to understand and therefore
> maintain.  We know exactly where to look for it, as opposed to any
> particular place in our JS source, perhaps even more than once, or as a
> result of many conditions.  

I actually think it's easier to have it in code. You can do it a couple 
of ways.

Have an initHandlers() to setup all event handlers and then you have 
them collected into one place and you don't have to scan either HTML or 
js to find them. Very nice.

Or, if you do something like OO widgets, you would attach them in each 
class since, when doing OO, every object should be responsible of their 
own behaviour.

And sometimes you actually generate the element structure on the client 
using DOM and then you're pretty much out of luck if you want to add 
them directly through markup.

Adding the event handlers in markup will just scatter code knowledge in 
more places and it will be harder to maintain. You should always try to 
keep your code as orthogonal as possible and that is not achieved by 
adding event handlers via markup.

I can understand that it sometimes can be hard to do it since you might 
be generating HTML based on database results etc but then I think you 
probably have a design problem on the server side if you can't _easily_ 
achieve that separation on the client side.

/Marcus



More information about the thelist mailing list