In the latter months of 2012, Microsoft released Internet Explorer version 10 for Windows 8. It was not available for Windows 7 or below at that point. It wasn’t until the end of February 2013 that IE10 was available for Windows 7. Now that it is out, it’s starting to reveal some issues with some of the ways that things may have been done in the past.
The major issue I’ve seen so far is that with IE version checking. It has always been a solid recommendation to base browser version information on testing of features, but in some cases, that simply isn’t practical. Specifically, one of my clients had some work that involved packaging their own fully contained javascript sdk. In such an environment, it has long seemed like a reasonable and safe fallback to rely on jQuery’s $.browser information. This call was deprecated quite a while back, but it has continued to be supported. That’s still true, but it became less true when jQuery released version 1.9 in mid January 2013, as that release is coming with an announcement that $.browser has been removed. That’s not the issue at hand in this post, but is worth noting, if you happen to be reading this and are looking for a way to fix your jQuery’s issues with detecting IE10.
Returning to the issue at hand, when IE 10 came out, some of the standard mechanisms for checking IE versions with $.browser suddenly ceased to work right (and this was the case for my client as well, which is why I know about it to begin with). The issue stems from the fact that $.browser stores the version number as a string. That sounds strange, but there is good reason for it. You see, a version number for most forms of modern software isn’t just something like IE version 10. Rather, it will end up looking much more like IE version 10.0.3.1.38.257. This allows the developers to expressly know exactly what build version is in use, to fine-tune new features and bug fixes. And since a number can only have one decimal, this has to be respresented as a string.
Thus the standard way to compare to a known major version is going to be comparing strings. Something along the lines of
if ($.browser.version < "7.0") //perform actions for old browsers...
That worked for a long time. But we now face an issue. You see, 10.0 is greater than 7.0, if you are comparing numbers. However when comparing strings, different rules apply. "10.0" is less than "7.0", in the same way that "Azxz" is less than "Bxz". Thankfully, there is a simple solution: convert the version back to a number, and then compare it to a numeric value. Since it is a safe bet that we won't have to worry about software versions in the thousands (for the major release number), we can look at the first three characters. That is important, since more than three characters would open us up to the possibility of having multiple decimals in the string, such as "8.1.xxx". Three characters gives us "8.1", which will convert to 8.1 (however four characters would give us "8.1." which would not execute properly).
The final code then for solving this problem is to adjust our earlier check to incorporate this process. So it becomes something along the lines of
if (parseInt($.browser.version.substring(0,3)) < 7.0) //perform actions for old browsers...
The one caution I want to stress about this is that it will absolutely fail if/when the related pages are updated to use jQuery version 1.9 or newer. But if you are operating on an older-than-the-latest-version, this could give you a quick fix for a rather troublesome problem. Enjoy!