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

Kowalkowski, Lee (ASPIRE) lee.kowalkowski at hmrcaspire.com
Wed Aug 31 08:18:31 CDT 2005


> -----Original Message-----
> From: Matt Warden [mailto:mwarden at gmail.com]
> Sent: Wednesday, August 31, 2005 12:20 PM
>
> No, completely separating the JavaScript to <script></script> tags
> would be the equivalent to using a stylesheet. 

Erm, no... If you use a stylesheet, you still need references to it in your
markup, we use id="foo" and class="bar" don't we?  Or do we only ever use
the default HTML element selectors? (good luck, I would encourage that
actually if it had better support).

> Stylesheets use
> selectors to apply style, 

DHTML uses event handlers functions to invoke behaviour.  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.  

We just shouldn't insert them via code without good reason.  Separating
behaviour from markup is the reason given but I don't think it's been
achieved, because I think the reason is misquoted, and the real intent is to
separate behaviour from presentation.  

Taking out all behaviour references from our markup doesn't achieve this.
Yes we have nice & clean markup, but our code is a lot dirtier.  

I think we can agree that it's counter-productive to attempt complete
separation of presentation and behaviour, because that's the nature of
DHTML, but let's have a sensible compromise!  I wouldn't strive for
perfectly clean markup at the expense of harder to maintain code, because
that code is more expensive to maintain than the markup.

> You're basically saying the
> JavaScript equivalent of:
> 
> <foo style="color: red;"> ... </foo>
> 
> is better than:
> 
> <foo style="color: red; font-weight: bold; text-align: center:
> vertical-align: top; float: left;"> ... </foo>

No, I'm not that silly.  I'm saying onsubmit="return validate(this)" is like
class="foobar"; where onsubmit="<loads of commands, not invoking a remote
function>" is like style="<inline style rules>".  So I'm saying that
class="..." is better than style="...", everyone should know that.

> Nothing about document.myForm says that there is <form name="myForm"
> ...></form> markup in the document. 
> It is referring to an element in
> the document tree. It just happens to ALSO be possible to "add" by
> "adding" in markup. But it's also possible a number of other 
> ways, e.g.
> 
> var frm = document.createElement('form');
> frm.name = 'myForm';
> document.appendChild(frm);

Yes, I know, I think I'm guilty of confusing markup with presentation too
now, but I wouldn't encourage the creation of presentation elements in
client-side code, over referencing behaviour in markup because we're not
separating presentation from behaviour either way, and I would stick with
Volkan's moral and keep it simple.

Thanks Matt, I appreciate the comments.

- 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