When there's only one

...there's only one choice
Everything here is my opinion. I do not speak for your employer.
April 2004
May 2004

2004-04-20 »

While you weren't looking, Javascript became a real programming language

Prototype-based object-orientation, component interfaces, first-class regexes and iterators, anonymous functions, a built-in HTML parser and streaming multi-connection, asynchronous HTTP retriever, an event-based programming model, and a built-in standards-compliant UI library (ie. the web browser). Not to mention a worldwide repository of open-source sample programs.

If you're like me, everything you think you know about Javascript is wrong. Last week I spent a few days actually reading through the latest relevant standards (HTML4, CSS2, DOM2, Javascript 1.5) and writing some test programs ("html pages") to try them out. I discovered two amazing things:

  • IE 5.5, Mozilla 1.5, and Konqueror 3.1 all mostly comply with all three standards. There is a common useful subset of those standards supported just fine, without random variations, by all three browsers. (Give up on Netscape 4 support. It's stupid. Nobody uses it anymore. If they do, just send the losers home.) By the way, Javascript is implemented just fine in all browsers. All real-life problems are mistakes in implementing CSS2 or DOM2, not Javascript itself.

  • You can write useful programs using Javascript and DOM2, and those programs aren't even ugly anymore. Really! I mean, you can say things like "document.forms.myform.mybutton.background = 'blue'", and you can pass buttons, forms, tables, rows, columns, or anchors into functions or objects that do things to them. How much more sensible can you be?

In the process of my testing, I wrote a function that easily sorts any HTML table by any column you click on, another function that automatically applies alternating CSS styles to alternating rows in all tables in your document, and another one that retrieves an HTML file, parses all URLs from its content, and dumps those into a listbox. It took a bit of work to "discover" the subset of DOM2/CSS2 that is supported by all browsers, but in the end I didn't need any weird browser-specific conditionals to make it work. You hear me? None!

Kudos to Microsoft

Congratulations to Microsoft, by the way, for getting more than 80% of the people in the world to use IE 5.5 and higher, and thereby nearly eliminating the need to support any non-DOM2/CSS2 browser. Microsoft may or may not have gotten their browser share by being evil - but they got people to upgrade their browsers by being amazingly good. IE 6.0 isn't noticeably slower than IE 4, it runs on Win98 and later (and I think IE 5.5 works in Win95, even), and upgrades are free. Those things put together mean that we can have what Mozilla promised but never delivered - a world in which everyone of interest is running a standards-compliant web browser.

CSS is designed by idiots

While Javascript and DOM2 are works of genius, I have nothing at all good to say about CSS. Without going into too much detail, I'll say that the following is an extension, added in CSS3 (yes, version 3.0 of CSS, which nobody supports yet) to allow odd-numbered rows to have a different style than even-numbered rows:

tr:nth-child(odd) { /* stuff */ }
tr:nth-child(even) { /* other stuff */ }

That's right. CSS is so bad at representing what it wants to represent that it needs a special extension just to be able to identify even/odd rows. Javascript is simple, easy, scriptable, and because of the lousy tag-matching model, more browser CPU time is spend rendering CSS2 than running Javascript that does the same thing. Somebody deserves to be shot.

I'm CEO at Tailscale, where we make network problems disappear.

Why would you follow me on twitter? Use RSS.

apenwarr on gmail.com