Hating on JS

Okay, so actually I'm very impressed with what enterprising coders have been able to do with fantastic libraries like NodeJS, PhantomJS, JQuery, mootools, etc. They've take away a lot of the pain and to them I'm very grateful. Making javascript work more in the fashion of other ECMA standard languages can only lead to better results.

But, can we tighten it up a little, please? I googled 'javascript gotchas' and returned 197,000 responses. Granted, I didn't scroll down to see how many of the hits were actually relevant but I did screenshot just the first page: The horror stories of pain and hours of lost productivity are enough to drive a CIO or Director straight to a room padded with rubber.

My own tale is simple in comparison to some of those stories through: I was coding a Ruby on Rails application and was all happy with the results I had achieved using Rails conventions and a smattering of jQuery to spruce up the front-end. Then, I was hit with the bright idea that I should really use some Ajax to only refresh a part of the screen. So, I read through the Rails UJS guide and quickly coded up the results I needed using partials. This is where things went awry... One of the buttons triggering the partial refresh stopped working and, worse, its behavior seemed erratic. Sometimes it worked and others it didn't. The fix turned out to be simple -- just change:

button.click(function(){...code...});

to

button.live('click', function(){ ... code ...});

And now, everything is working as planned. Remember, when you're slogging through that weird JS bug that you are not alone. We've all been there. So take a break and read through some of the items above. Some will make you laugh and some will make you cry. Now, roll up your sleeves and slay that bug and thank all the dedicated coders who have made JS a little easier to use.