undebugged javascript

rjbs on 2005-01-05T21:43:58

It makes me sad that MSIE has no useful JScript debugger. There's a debugger, but it's horrible and approaches useless. There's also no equivalent to Mozilla's DOM Inspector, which is wildly useful.

Because of this, I tend to write my JavaScript code while debugging and exploring with Firefox. When it works, I take it over to MSIE to see how much I need to tweak to make it work. That part of the exercise is torture, and involves a lot of trial and error and blind flying. One good thing about it is that I tend to learn a number of ways to solve any one problem: the first three ways don't work in MSIE, but the fourth does and once it's working I know a bunch of new things about JavaScript.

I wanted to get some things done at work that have been shelved for a while; basically, I needed to tie together a few of my previous JS hacks to make a single dynamic form for creating semiconductor specifications. (I'm simplifying, here.) I needed a form that could have a variable number of entries providing attribute/value pairs, and I needed the value input widgets to be appropriate to the attribuet being entered. So...

http://rjbs.manxome.org/hacks/js/all_together.html

It should be refactored and made easier to build from parts. Right now, a lot of the JS is in the template for the production version of that form. Still, it works. It started working around quarter to two, but only in Firefox and Safari/OmniWeb. MSIE quietly ignored the "(other)" option on the combo box. This was weird, since my combo box code worked in MSIE.

I spent the next two hours poking around, and I determined these things: MSIE will not (but other browsers will) respect this kind of code:

selectElement.setAttribute("onChange","function_name(this)");

MSIE will not, as far as I can tell, let me setAttribute a function to onChange or onchange, either.

I can create an event listener and attach it to the object, but I need to produce a fair amount of code to do that, because the MSIE event model is different from the DOM2 model. It struck me as overkill to write twenty or thirty lines of code to say "attach X to event Y on object Z."

MSIE will respect code like this:

selectElement.onchange = function() { function_name(this) };

So that's what I did. There was a point in my groping around that I tried to use a generator to produce that function, but it didn't work. I may try that again, later, when I'm cleaning things up.

The more JavaScript I write, the more I enjoy writing it. I also get more annoyed with its strange quirks, which I should write down. Of course, given that my use of JS is so minimal, the quirks I see are probably nothing compared to larger-scale issues. Here are two simple ones:

It blows my mind that replaceNode is called with (newNode, oldNode); it seems like a mistake to not take (oldNode, newNode) because the comma could then be read or thought of as "with."

The behavior of the for/in loop is horrible. Given an object, it loops over properties. Given an array, it loops over indices (not elements). Given an object that "acts like an array" it loops over properties. Really, all the objects that are supposed to act like arrays suck.


other uses for JavaScript

lachoy on 2005-01-06T14:11:54

Using JavaScript as an embedded interpreter for Java is pretty useful as well, and you don't have any pesky browser issues to deal with. I use Rhino from the Mozilla project in a testing framework and it's quite easy to use Java objects in your JavaScript code -- I posted about it recently with code examples.

Also interesting (if weird) is the E4X (ECMAScript for XML) specification -- Jon Udell wrote about it a few months ago. I haven't had a chance to use it yet -- apparently it's been implemented in both the C- and Java-based JS interpreters from Mozilla.

Re:other uses for JavaScript

rjbs on 2005-01-06T14:49:29

That looks like another reason to like JavaScript -- and dislike Java. I can write code to easily use in Java, in JavaScript? Great! I can't benefit from JavaScript's dynamism by replacing methods? Ergh.

I should "finish" learning Java (as much as I've learned Ruby or Python) so I can complain with more authority. Maybe now that I have a Mac, on which the Java system is less annoying. I hope.

Anyway, in the end, I mostly write JS for work, where it has to run on MSIE. I said to my boss, wistfully, today, "I wish we could just use Firefox internally."

I was just spewing, but he said, "I'm not sold on it. It locks up on me more than IE. And I don't like tabs."

Re:other uses for JavaScript

mary.poppins on 2005-01-10T00:47:23

Learning Java is just a waste of time. There are more interesting things to learn out there, such as SML (mlton), ocaml, and haskell. :)

Unless you want resume fodder, I suppose.

I learned Java once, but have thankfully forgotten most of it.