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

Kowalkowski, Lee (ASPIRE) lee.kowalkowski at hmrcaspire.com
Tue Aug 30 09:48:20 CDT 2005


> -----Original Message-----
> From: Matt Warden [mailto:mwarden at gmail.com]
> Sent: Tuesday, August 30, 2005 1:42 PM
> 
> Kowalkowski, Lee (ASPIRE) wrote:
> > Excuse my ignorance, but when did it become preferred to do 
> any of that over
> > adding the event to the HTML itself?
> 
> The idea of separating behavior from the markup has been taking off
> for the past year or so. Basically, the thought is not that these
> handlers should only be attached for those users who have JavaScript
> enabled, and thus it is to be done via script.
> 
> See e.g.
> http://www.onlinetools.org/articles/unobtrusivejavascript/chap
> ter1.html

Thanks Matt, I understand that.  I agree that you shouldn't author
browser-based material that assumes javascript capability.  But adding
onsubmit to a form element directly doesn't assume this, and would degrade
gracefully.  The unobtrusive javascript article says using onsubmit in the
HTML *is* the unobtrusive way to do it, the obtrusive way was an onclick
attribute on a non-submit button.  

I think unnecessary post-parse programmatic manipulation of the HTML DOM is
equally obtrusive, and even harder to follow / debug / maintain.  This
practice reminds me of self-modifying code or code injection, and  would
definitely have some common disadvantages.

If I have <form onsubmit="return validate(this)">, have I separated
behaviour from markup?  Almost; the behaviour is in the validate function,
but my markup contains an invocation.

If I change it to do the javascript: document.myForm.onsubmit =
myFormValidation, have I separated behaviour from markup?  Not as much - I
have an explicit markup reference to "myForm" to which I am programmatically
referring.  I didn't have to explicitly refer to it before, because it was
being passed as an argument.  I also have to ensure I get my timing right so
I don't try to define the onsubmit handler before myForm has been parsed,
which means for a short time, myForm does not have an onsubmit event
handler.  Onload events can take a while to trigger, especially if there's
something on the page taking a long time to load.

So after doing this, the behaviour and markup are more closely coupled!  How
Ironic.

I still think that Volkan's tip is valid, it highlighted a significant
difference between IE and Mozilla.  I was just curious as to why he even
went there!  I'm still not convinced it's worthwhile.

- LK


===========================================================
Our e-mail domain has now changed from iraspire.com to hmrcaspire.com. Please update your address books.
===========================================================



More information about the thelist mailing list