Javascript — Some Thoughts

technologyinternet explorer, javascript, and work

For most of the last 9 months, I’ve been working on a project called InterRisk; specifically, developing a web portal for viewing scientific data overlaid on a map. (The scientific data is marine-related, in our case mostly to do with algae levels around the UK and Irish coast). Anyway, most of the coding has been in Javascript; there was some PHP, but I’m trying to avoid it, and any new server-side code will probably be in Python because it’s awesome.

Before I started, I’d done very little javascript, just a few toy programs and whatnot. Mostly my experience of it was through bloody annoying websites that use it unnecessarily.

What I’ve learned, though, is that it’s actually a reasonably decent language — yes, it has some misfeatures, and some things don’t behave as I think they should (for (i in object), for example, which sets i to the index of the member, not the member itself like in python or sh). They’re fairly minor, though, in my opinion, compared to some of the good features, like functions being first-class objects that you can pass around, for example as event handlers. (I know that quite a few languages can do this, but it was a surprise to learn that Javascript could).

The real spoiler, though, is browsers. If I could write an application that only had to support one browser, I’d be reasonably happy. At least, if the browser in question wasn’t Internet Explorer. The variability in javascript support in the various browsers, though, is a serious issue; there are so many features that just can’t be used because Internet Explorer 6 still needs to be supported (or even IE7, which as far as I know has almost identical javascript support to IE6). I don’t know if IE8 will be an improvement, but it’s hardly important — it’ll be years before IE6/7 are sufficiently rare to make using newer features safe in a portable application (mostly because there are organisations still using IE6, after more than two years — I understand the arguments for not rolling out new software immediately, especially in large corporate environments, but two years? What the hell are you doing?).

(There is a bright side — as I have no access to any IE6 machine at work, I have an excellent excuse not to support it. As mentioned, I don’t think it makes a difference where javascript is concerned, but it’s one less thing to worry about.)

Even discounting Internet Explorer for the moment, things aren’t perfect; I’ve run into problems because, for example, Firefox allows things that Opera doesn’t, when Opera’s behaviour is actually the correct behaviour (but of course, I can’t code to Opera’s strict standards-compliance, because discounting Internet Explorer was only wishful thinking, and IE7 doesn’t support the correct way of doing things that Opera mandates).

Not supporting large chunks of the DOM (DOM level 2 or 3, if I remember correctly) is okay, though, because I can just extend the Node object to provide wrappers — if getElementByTagNameNS isn’t defined, fake it using getElementByTagName instead. But oh, wait, Internet Explorer doesn’t let you extend the built-in DOM objects. Why? No idea, but it means that I have to write a wrapper function for every browser to use, even if it supports the necessary methods natively. So much for that plan.

Then, when something goes wrong — and it will — you’re sort of stuffed. While Firefox has Firebug, which isn’t perfect but does the job well enough, and Opera has Dragonfly which was great while it worked but seems to have broken for me, Internet Explorer has…nothing. As far as I know. Extensive Googling has, at least, turned up nothing. IE7 seems to have only about two or three javascript error messages anyway (which at least is more than ed, which makes do with one — ? — but they aren’t much more helpful), so it’s back to alert() and popping up the value of variables at a dozen different points throughout the application. (I assume Microsoft expect people to fork out for Visual Studio or something to get access to a debugger. Sod that, all I want is useful error messages and a DOM inspector)

Anyway, this has turned into more of an Internet Explorer rant than a Javascript rant. As I mentioned, no browser is perfect, but some are less perfect than others, and as Internet Explorer is still ⅔ of the browser market, it’s proportionately more of an issue than Opera or Safari, or even Firefox, would be if their javascript implementation was as poor.

Update: Simon points out that IE8 does no better on the Acid3 test than IE7, but suggested the Internet Explorer Developer Toolbar from Microsoft; I’ve not yet had a chance to test it.