jQuery: Internet Explorer is not always wrong

jQuery: Internet Explorer is not always wrong

Internet Explorer is not always wrong when it interprets our jQuery code.

A couple of days ago a friend of mine on Facebook asked me some help with a jQuery slideshow installed as a Joomla plugin. This slideshow worked just fine in Chrome and Firefox but it didn't start in Internet Explorer. My first impression was that we were facing another IE's bug. But I was wrong.

It turned out that one image was missing from the slideshow set. When you reference a DOM element and this element doesn't exist, JavaScript's implementation of the DOM specifications must return a null value.

When you try to use a null node, the JavaScript interpreter must raise an exception. The slideshow code fetched the left offset of each slide programmatically but when it encountered the empty reference to the node, the routine:

var left = parseInt(element.css('left')) + 'px';

returned instead NaN because a null value cannot be computed. Chrome and Firefox seemed to ignore this error, but IE actually stopped the entire slideshow.

Surprisingly, IE's interpretation is absolutely correct: a DOM exception is not recoverable and a browser is allowed to stop the script's execution. This is exactly what IE did in this case.

Sometimes we think that "bad guys" will be always the same. Well, not always.