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. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your
    weblog? My blog is in the exact same niche as yours and
    my visitors would definitely benefit from some of the information you provide here.
    Please let me know if this alright with you. Many thanks!

  2. Greetings from Florida! I’m bored at work so I decided to browse your website on my iphone during lunch break.
    I really like the knowledge you present here and can’t wait to
    take a look when I get home. I’m surprised at how fast your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyways, superb blog!

  3. I all the time used to study post in news papers but now as I am a user of internet
    therefore from now I am using net for articles or reviews, thanks to web.

  4. Definitely imagine that which you said. Your favorite justification appeared to be at
    the net the easiest thing to keep in mind of. I say to you,
    I definitely get irked whilst other people consider issues that they plainly don’t realize about.
    You managed to hit the nail upon the highest and defined out the whole thing without having side-effects , other people
    could take a signal. Will probably be again to
    get more. Thank you

  5. Admiring the persistence you put into your site and
    detailed information you offer. It’s nice to come across a blog every once in a
    while that isn’t the same old rehashed material. Wonderful read!
    I’ve saved your site and I’m including your RSS feeds to my Google account.