jQuery CDN as A Named Module In RequireJS

If you want RequireJS to fetch jQuery from a CDN, you’ll need to tell it to do so using the “paths” config:

require.config({
paths: {
"jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
}
});

As I understand it, if you want to use the local file fallback code a la html5bp you’re out of luck. For those of you who don’t know, the local file fallback code allows you to load jQuery from your own domain if the CDN version fails to load. It looks like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.2.min.js"><\/script>')</script>

Arguably, if you’re using jQuery it is likely to be a global requirement rather than a requirement for one or two modules. The same sort of thing with BackboneJS. It might be better to place these global requirements in the HTML for your pages (before the RequireJS script) to save typing ‘jquery’ and ‘backbone’ in all your module definitions.

22,429 thoughts on “jQuery CDN as A Named Module In RequireJS

  1. It’s appropriate time to make some plans for the longer term and
    it is time to be happy. I’ve learn this put up and if
    I may I wish to suggest you some attention-grabbing issues or suggestions.
    Maybe you could write next articles referring to this article.
    I desire to read even more issues about it!

  2. Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Any way I will be subscribing to your augment and even I achievement you access consistently fast.

  3. I enjoy what you guys tend to be up too. This kind of clever work and reporting!
    Keep up the amazing works guys I’ve incorporated you guys to my own blogroll.

  4. Does your blog have a contact page? I’m having trouble locating it but,
    I’d like to shoot you an email. I’ve got some suggestions for your blog
    you might be interested in hearing. Either way, great blog and I look
    forward to seeing it improve over time.

  5. I just like the valuable information you supply on your articles.

    I’ll bookmark your blog and test once more here regularly.
    I’m somewhat certain I will be told plenty of new stuff proper here!
    Best of luck for the next!

  6. What’s up all, here every person is sharing these kinds of know-how, so it’s nice to
    read this website, and I used to pay a quick visit this
    blog every day.