views:

95

answers:

2

Suppose CSS as we know it had never been invented, and the closest we could get was to do this:

<script>
// this is the page's stylesheet
$(document).ready(function(){
    $('.error').css({'color':'red'});
    $('a[href]').css({'textDecoration':'none'});
    ...
});
</script>

If this was how we were forced to write code, would we put up with it? Or would every developer on Earth scream at browser vendors until they standardized upon CSS, or at least some kind of declarative style language?

Maybe CSS isn't perfect, but hopefully it's obvious how it's better than the find things, do stuff method shown above. So my question is this. We've seen and tasted of the glory of declarative binding with CSS, so why, when it comes to the behavioral/interactive layer, does the entire JavaScript community seem complacent about continuing to use the kludgy procedural method described above? Why for example is this considered by many to be the best possible way to do things:

<script>
$(document).ready(function(){
    $('.widget').append("<a class='button' href='#'>...</div>");
    $('a[href]').click(function(){...});
    ...
});
</script>

Why isn't there a massive push to get XBL2.0 or .htc files or some kind of declarative behavior syntax implemented in a standard way across browsers? Is this recognized as a need by other web development professionals? Is there anything on the horizon for HTML5?

(Caveats, disclaimers, etc: I realize that it's not a perfect world and that we're playing the hand we've been dealt. My point isn't to criticize the current way of doing things so much as to criticize the complacency that exists about the current way of doing things. Secondly, event delegation, especially at the root level, is a step closer to having a declarative behavior layer. It solves a subset of the problem, but it can't create UI elements, so the overall problem remains.)

A: 

JavaScript is tolerated only because it's everywhere. If it were actually a good language then everybody and their dog wouldn't be using it to create esoteric DSLs that try really hard to make you forget you're using JavaScript.

Azeem.Butt
If "good language" and "everybody's using it" would correlate in any way, no one would be using PHP. JavaScript *is* in fact a very good language, that things like jQuery can exist proves the point. That unaided DOM manipulations are such a pain is not JavaScript's fault.
Tomalak
Esoteric DSLs are usually hiding not Javascript (which is good), but environment (which varies from inconvenient to terrifying)
ymv
I don't think you have any idea what facts or proof are, but thank you for pointlessly defending the one language you kinda have a little bit of experience with, maybe.
Azeem.Butt
+1  A: 

*Why for example is this considered by many to be the best possible way to do things: $(document).ready(function(){ $('.widget').append("..."); $('a[href]').click(function(){...}); ... }); *

Because this isn't simply behaviour. The proper way to execute the example you provide is with plain html. What you describe is appropriate for layering of additional behaviour in the manner of progressive enhancement. This is inherantly complex. This is the behaviour of behaviour.

graphicdivine
Perhaps, but my main point is that it's procedural rather than declarative. What we need is a way to declare that all .widget elements get an a.button element, rather than execute a DOM query, iterate and append.
greim