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. Have you ever considered publishing an e-book or guest authoring on other sites?
    I have a blog centered on the same information you discuss and would love to have you share some stories/information. I
    know my viewers would value your work. If you are
    even remotely interested, feel free to send me an email.

  2. Howdy! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really enjoy your
    content. Please let me know. Thank you

  3. I know this if off topic but I’m looking into starting
    my own blog and was curious what all is needed
    to get setup? I’m assuming having a blog like yours would cost a pretty penny?

    I’m not very internet savvy so I’m not 100% positive. Any recommendations or advice would be greatly appreciated.
    Cheers

  4. Great blog! Do you have any helpful hints for aspiring writers?
    I’m planning to start my own blog soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for a paid option?
    There are so many options out there that I’m totally overwhelmed ..
    Any ideas? Many thanks!