jQuery and IE… Again

So, at work today, I discovered another interesting facet of jQuery and IE.

This is in specific regards to jQuery 1.4.2 and IE7. I haven’t tested this in other versions of IE, but it’s probably as applicable. For jQuery, this will only be a problem in 1.4.2+, as earlier versions of jQuery don’t support this feature.

Let’s get to it.

The situation

I’m generating some HTML forms on-the-fly by passing some JSON objects to a function that returns a jQuery object, which I then append to a modal dialog. It’s pretty elegant, and I may release it as an open-source jQuery plugin at some point. Anyway, part of this function uses jQuery 1.4.2’s ability to generate jQuery DOM objects when HTML strings are passed to the function. Example:

$('<div class="myclass" />');

This is great because it allows me to create a string of HTML and pass it to jQuery, effectively increasing the speed of the program by avoiding function calls (can you imagine calling the attr() method for every attribute, and then the append() method for every element, including child elements??).

The problem

So what’s the problem? Well, this works in IE, so you’d think there isn’t a problem. In fact, I was also convinced (read: tricked) that IE handled this fine, as initial testing returned the expected results. However, because sending an empty string to the jQuery function returns an empty jQuery object, debugging this type of code can be very dangerous, as instead of generating errors, the script will die or stop functioning silently. No errors makes debugging very difficult!

I was using the following code to generate a form tag, append it to an object, and then return it. It looked something like this:

var newForm = $('<form method="post" action="">');

This works in other browsers, but IE chokes. Silently. I discovered this when I started doing some testing on generating the object, independent of my script (which I’d been attempting to debug for well over an hour at this point).

Alerting the length property of the jQuery object has been helpful here. I first tried $('<div />').length and got the result 1. That makes sense. Then I tried $('<div>').length. This also, correctly, returns 1.

Then I tried something else. Since all this hypothetical testing was driving me bananas, I tried the actual form HTML I was using. $('<form method="post" action="">').length returned… 0!! How frustrating! Taking the attributes out and simply using $('<form>').length returns 1 as well, however. To get the correct result, I had to use the self-enclosing form tag: $('<form method="post" action="" />').length. This returned the correct result (1). Finally. That one character fixed my entire program in IE. Wow!

In Summary

TL;DR version: IE chokes and returns an empty jQuery object when you pass an HTML string that has attributes without a self-closure. I hope this saves some folks valuable time debugging this ridiculous phantom. I’m curious if this should be filed as a jQuery bug or not.

Happy coding!

June 14th, 2010 | Remark

New Design

Yep, I’ve yet again redesigned. If you’re using Firefox or Chrome, the site will be beautiful, and if you’re using Safari, it’ll be almost as beautiful (Safari 4 still doesn’t support inset box shadows in CSS3 *sigh*).

So a few details about the design.

  • Grayscale. The color one chooses when he does not know which color to choose.
  • Heavy CSS3. If you look at it in IE, it’ll probably puke. Sorry. Upgrade your browser. =)
  • Design took place amidst other activities, including a very lonely The Office marathon.
  • The comment form uses an ordered list. The comment form button is pretty CSS3 stuff.
  • The web site makes heavy use of alpha transparency, both in PNG graphics and background colors/gradients. This means that the design can be completely transformed, in full color, simply by applying another background image (yes: another).
  • Expect to see the background change from time to time as I get bored with the grayscale.
  • The home page 3-column layout is some cool CSS3 trickery.
  • All of the designing was done in the browser, not in Photoshop. I think this approach dramatically changed my attention to detail, particularly the fonts and colors (well, shades, if you will).
  • I took a minimalist approach with the design and stripped out almost all of the plugins I was using for Wordpress. I’m much happier with my content and a few pages, and I think the refocus on “less is more” allows me to write more, fix less, and by extension allows folks to read more and be confused less.

If you’re a graphic designer and would like to see your work on my site, drop me a line. ;-)

Well, that’s all for now. =)

Update: Okay, here’s more. Pick a color to see how the design changes.

June 3rd, 2010 | Remark