Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

rjbs (4671)

rjbs
  (email not shown publicly)
http://rjbs.manxome.org/
AOL IM: RicardoJBSignes (Add Buddy, Send Message)
Yahoo! ID: RicardoSignes (Add User, Send Message)

I'm a Perl coder living in Bethlehem, PA and working Philadelphia. I'm a philosopher and theologan by training, but I was shocked to learn upon my graduation that these skills don't have many associated careers. Now I write code.

Journal of rjbs (4671)

Wednesday January 05, 2005
04:43 PM

undebugged javascript

[ #22583 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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 [mozilla.org] 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 [perl.org] recently with code examples.

    Also interesting (if weird) is the E4X [ecma-international.org] (ECMAScript for XML) specification -- Jon Udell wrote about it [infoworld.com] a few months ago. I haven't had a chance to use it yet -- apparently it's been implemented in both

    • 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, to
      --
      rjbs
      • 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.