On the Soapbox

« This is why I no longer watch CBS | Main | Dell, Gateway, and RAID-0 »

Fun with JavaScript

Wednesday, October 4, 2006
Keywords: Technology

I wrote a Firefox extension yesterday. It is not anything earth-shatteringly great. Just a simple function that I had wanted and that I had wanted to be implemented with an extension that was very simple, small, and lightweight. There were only a few dozen lines of code, and it was more of a two-hour learning exercise.

Now what was interesting about this experience was that I got to work with something that I hadn't touched since 1998: JavaScript, and this post is mostly about that experience. But before I begin, I should do a quick backgrounder. A large number of people do not know that most Mozilla products are kinda like elaborate webpages. At the core is the Gecko rendering engine, written in C and natively compiled. This handles all the grunt work and handles things that are specific to the operating system, like dealing with graphics (GDI+, OpenGL, etc.), the file system, etc. In the case of Firefox (or Thunderbird, Sunbird, Songbird, Seamonkey/Mozilla, etc., but not Camino), the browser itself, however, is written in a markup language called XUL. Like XHTML, it is a XML-based markup language, and it works an awful lot like HTML but with different tags. And Firefox is just one really big and elaborate collection of XUL, so Firefox is in a way akin to a big collection of HTML webpages. And just like HTML, the style and layout in XUL is controlled by CSS. This, by the way, is what makes up Firefox skins: they're just a collection of CSS files and graphics, and it's the CSS files that specify where things should be placed, how much spacing there is between various items, what colors to use, what images to use, etc. The brilliant thing about this setup is that it's cross-platform. Just as a webpage will look the same in Windows as it does in Linux, the XUL user interface is equally cross-platform. And since they already have Gecko to render webpages, why not save themselves the trouble have Gecko render the user interface too? It is also brilliant in that it provides for a fairly clean separation of the front-end user interface from the back-end, and XUL+CSS makes changing the UI and custom skinning much easier than most other skinning systems because a lot of people are already familiar with HTML+CSS.

While XUL+CSS is, in my opinion, ingenious, the Mozilla way of doing things does have one soft spot. A webpage isn't just about layout and design. Function is what matters, and something's gotta happen when a user clicks on a button or selects an option or else all you've got is a pretty picture. This is where the JavaScript comes in. Yes, the Firefox user interface is powered by JavaScript. Click "OK" in the options dialog, and you're calling up a JavaScript function that saves the options that you selected. From a practical perspective, I can see why Mozilla went with using JavaScript. It fits this "webpage" style of software, it means that they can just reuse the JavaScript interpreter already in Gecko instead of scratching out a new language, it--along with XUL and CSS--makes working on Firefox and Firefox extensions relatively easy (at least compared to say, working on an IE extension; you just need a Notepad and WinZip; no compilers needed!), and it means that they can rely on the great pool of existing JavaScript programmers. But hold on a minute... JavaScript programmers?

As the name would suggest, JavaScript is a scripting language that borrows some of the style of Java (though the two are unrelated). With the rise of Firefox and with the rise of AJAX, people are starting to take JavaScript a bit more seriously instead of snickering at the very concept of a "JavaScript programmer", but that doesn't change the fact that the language is, fundamentally, a tacky curiosity. Putting JavaScript's following of Java's moronic dogma towards the OO style, the fact that JavaScript was originally conceived as a lightweight language scripting language designed for trivial frills is evident. This is a language where, even today, changing the third byte of a string requires blowing up the string and manually piecing it back together. Ugh.

Needless to say, my experience with coding up the extension didn't go quite as smoothly as I had hoped. I hadn't coded JavaScript since 1998, and I needed to check the language reference, but that's okay. Every programmer needs to consult a language reference. The frustration was just dealing with the quirks and limitations of the language itself. For example, the ability to access a character in a string using [ ] but not being able to modify it that way is a rather annoying limitation of the language, and a very counter-intuitive one at that. While there have been efforts made over the years to mend JavaScript's shortcomings, the need to maintain backwards compatibility means that JavaScript can never be truly and fully reformed. JavaScript is, IMHO, a bastard language that should have been aborted at birth, but unfortunately, it has survived. Fortunately for me, my extension was simple and short, so I only had to deal with JavaScript for a couple of hours, and since I do not intend on becoming a "JavaScript programmer", I don't think I will have many more run-ins with this language. I do, however, have some newfound respect for the poor souls who do have to do major amounts of coding in JavaScript. At least people working on the Firefox interface using XUL+CSS+JS are lucky: they don't have to deal with the fact that IE, Opera, Safari, and Mozilla all handle JavaScript differently. It's a wonder how people who do AJAX haven't jumped off a cliff yet.

Comments
Post a comment »

No Comments

Leave a Comment

Name:
E-mail Address: (not displayed)
Comment:

Auto-formatting notes: Please separate paragraphs with one or more blank lines (i.e., double line breaks; single line breaks will be converted to BR tags). URLs will be auto-linked. The following HTML tags are allowed:
A, ABBR, ACRONYM, ADDRESS, B, BIG, BLOCKQUOTE, CODE, EM, H[1-6], I, IMG, LI, OL, PRE, SMALL, STRIKE, STRONG, UL