Using RequireJS to Load the Right Version of jQuery Depending on Internet Explorer Version

Here’s a cool trick you can use with RequireJS. I found it in a post by @rnsloan called Conditionally Loading jQuery 2.x.

Since environments with SharePoint can often have a mixed bag of browser versions, this conditional setting lets us load jQuery 2.x when the browser can handle it (generally IE 9+) or jQuery 1.x when it can’t. The function document.addEventListener is undefined in IE8 and earlier – sometimes called oldIE – and defined in IE9+ – also known as modern.IE – so it’s a simple little test that ought to work.

If you don’t know why this matters, check out the Browser Support page on the jQuery site.

requirejs.config({
  paths: {
    "jquery": (document.addEventListener) ?
      ['//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min','jquery-2.1.3.min']
      :
      ['//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min','jquery-1.11.1.min']
    }
});

Note: The versions of jQuery above were current as of this writing, but may well have already moved on by now.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.