Update: Added the song "IE is being mean to me" at the bottom of the post.

I don't usually write "raw" JavaScript code, that is, JS code that does not use an existing framework - mainly because using code that has been tested in multiple browsers is better in most cases than writing your own code from scratch and testing it with different browsers.

However, every now and then it seems a project is small (and non-commercial) so that using a framework would be overkill. These are the times when the differences between Internet Explorer's JavaScript support and that of other browsers is painfully obvious. Here are two relatively recent examples and solutions:

1. The IE innerHTML and "Unknown runtime error"

The JS engine in IE is notoriously unhelpful when encountering errors. One thing in particular has bit me in the ass so many times it is not funny anymore. This is the occurrence of a "Unknown runtime error" when attempting to assign a value to an element in IE via the innerHTML attribute. For example:
document.getElementById('containing_element_id').innerHTML
    = 'A link text';
Looks innocent, doesn't it? However, in IE this code may or may not fail with an unknown runtime error depending on the context. For example, if another link is already applied to the containing element, then this code will fail on IE. Firefox, Opera and Safari will perform some code cleanup and allow this to work as expected (the content of the element is changed to that link). IE, on the other hand, fails with little warning. The point is, IE is very strict when it comes to nesting elements - in the specifications, nested links are not allowed. This is not a bad thing in my opinion - standards ought to be followed. The problem is, IE will not give you any clues as to what went wrong. If the code is more complex than in the example, and it mostly is, then this will take a long time to correct. There is one way to diagnose whether this problem is due to a previous tag which ought to closed per specifications: in IE8, you can use the debugging tools (F12) and set a breakpoint at the offending line of code. If the element itself can be retrieved via getElementById, then the problem is probably with a runaway tag somewhere earlier on the page. Once you find the offending tag (or the logic that generated it), you can fix the problem.

2. The IE extra comma error

A very painful bug that took me long time to hunt down was related to how IE handles object initialization. In any complex application, you are most likely generating JavaScript code or setting JavaScript variables from the server using some other programming language, for example by generating JSON data structures via PHP:
foreach ($a as $k => $v) {
   $result[] = json_encode($k).':'.json_encode($v);
}
   $data = '{' . join(',', $result) . '}';
At some point this data is combined and outputted to the browser as JavaScript code. Sometimes there will be cases in which an additional comma (",") is left on that JSON string because of some data not being included. In other browsers, this is not a problem: Firefox for instance can handle code like:
var js_arr = {"name1":"value1","name2":"value2","name3":"value3",};
See that extra comma at the end? It will cause IE to fail silently and prevent the rest of the code from being executed! You can imagine how much fun it is to spend a few days debugging complex AJAX UI code to find that a single additional character was the problem. And that can take more than a while, particularly if one does not know what to look for. And once you find the problem, you will not know whether to scream or to cry. My advice? Stick to tested code and be aware of these errors when you start debugging to avoid going on a wild goose chase.

IE is being mean to me again

Somebody linked to this, I think it's awesome and probably appropriate if you arrived here via Google:

"I tried Jquery, Moo tools and Prototype, but IE still won't display my widget right

and have you experienced the horror of debugging in Internet Explorer?

I'd like to say that I simply do not care, but how can I ignore so much market share?"

httpv://www.youtube.com/watch?v=vTTzwJsHpU8

Comments

Ale: Thanks for the song. I really feel identified :)