If you haven’t heard yet, last week the jQuery team announced on their blog that jQuery 2.0, which is scheduled for an early 2013 release, “removes support for IE 6/7/8 oddities such as borked event model, IE7 ‘attroperties’, HTML5 shims, etc.”
Because many people had a bit of a fit over that, the jQuery team posted a follow-up that addresses some common concerns and whatnot. In addition, a SitePoint article discussed the situation, but (uncharacteristically) that article and some of the comments are inaccurate.
So in this post, I’m just briefly showing everyone a simple solution for serving a different version of jQuery (or really, any script) to Internet Explorer versions 6-8.
Here it is, as shown on the jQuery blog, using conditional comments:
<!--[if lt IE 9]> <script src="jquery-1.9.0.js"></script> <![endif]--> <!--[if gte IE 9]><!--> <script src="jquery-2.0.0.js"></script> <!--<![endif]-->
Going by SitePoint’s incorrect version, it’s true that non-IE browsers wouldn’t see any script. But notice that the second script tag in the jQuery blog version of the code (shown above) is wrapped in conditional comments that are slightly different from the conditional comments wrapping the first script tag.
Here, let me make it a little more clear:
<!--[if lt IE 9]> <script src="jquery-1.9.0.js"></script> <![endif]--> <!--[if (gte IE 9) | (!IE)]><!--> <script src="jquery-2.0.0.js"></script> <!--<![endif]-->
Notice now I’ve added a second expression, separated by the “or” operator (the vertical pipe). This is not needed, because the end result will be the same: The second script tag is seen by IE9 and non-IE browsers. The added expression, although useless, actually makes the code easier to read from a developer perspective. Thus, although it’s more code, I tend to feel this is actually a better solution.
Here are some random notes regarding this issue:
- The script tag that the non-IE browsers see is wrapped inside what is referred to as a downlevel-revealed conditional comment; HTML5 Boilerplate uses this for its last conditional class
- As SitePoint correctly pointed out, IE10 does not support conditional comments; so technically you could just do “if IE9” instead of “if gte IE9”
- The jQuery blog follow-up post pointed out that jQuery 1.9 will continue to be supported until IE8 is no longer an issue in browser stats
- Obviously, this solution is pretty useless for jQuery until version 2.0 actually gets released, but if nothing else this is just another reminder of the quirks of conditional comments
- This announcement by the jQuery team makes it clear that you should never link to something like
http://code.jquery.com/jquery-latest.jsin production (something I’ve written about before)
If anyone has anything further to add, or has a recommendation for a better solution, I’m glad to update this post.