Separating Behavior From Presentation | July 26, 2004
I’ve developed a renewed interest in Javascript the last couple of months, due to articles like these, and more recently, an excellent SkillSwap talk by Jeremy Keith and Richard Rutter.
Over the weekend I decided to make a few little enhancements to the comment form on this site. I tidied up the live preview and added a “Make It Bigger” button to resize the comment field.
I really like the idea of separating behaviour from presentation. On a very basic level the site should still work smoothly if you turn off Javascript, CSS or both of them. For instance, at the moment, If you turn off Javascript, the “Make It Bigger” button won’t display, neither will the live comment preview. However I feel this separation should happen in the code as well as for the benefit of the user.
While scripting this button I realised there were essentially two ways of doing it. I could either affect the style of the form element directly using the style property, or I could apply a class to the field. If I use Javascript to affect the styles directly, that would be mixing presentation into the behaviour layer. To return the form element to it’s initial size I’m setting it’s height to be 100px. If I choose to set the initial height to something different in the stylesheets I have to remember to also alter this in the Javascript.
To get round this I thought about setting a class instead of directly setting a style. That solves the problem of presentation being mixed in with the Javascript. However it presents another problem. Turn CSS off but keep Javascript on and, even though the “Make It Bigger” button gets displayed, It won’t actually do anything.
So it strikes me that, while it’s possible to create separation for the user, the act of setting styles using Javascript makes this separation impossible for the developer.
Posted at July 26, 2004 9:03 AM
Colly said on July 26, 2004 10:25 AM
Just had to try it out, Andy - the form looks smashing on Safari. I had a few complaints early on about the ‘live preview’ on my site - mostly due to my poor Javascript skills. However, with a few tweaks and a little explanation for the user, the problems have dried up.
Increasing text field size is a must. I don’t know if you’ve used Basecamp from 37 Signals, but it’s the kind of thing they do as standard, and our clients have found it to be a real bonus when copying large chunks of text into the field.
Anyway, on a day when Keith is discussing attention to detail in our designs, these subtle tweaks seem highly appropriate.