Dynamic Loading of jQuery Libraries

I’ve been going back and forth with my pals @jbhoward (Jim Bob Howard),  @iOnline247(Matt Bramer), and @webdes03(Michael Greene) today about how best to dynamically load jQuery libraries.  We were all interested in the possibility of determining on the fly whether to load the jQuery library itself conditionally from an external CDN (like the Google Ajax Libraries API or the Microsoft Ajax Content Delivery Network).

So I’m thinking about adding a new, separate JavaScript function into the SPServices project for this.  What would your requirements be?

Here’s what I’m thinking so far for options and behavior:

  • An array of src locations for the jQuery library. An attempt would be made to reach each, in order, and the first successful connection would be loaded.
  • A location for the rest of the plug-ins and libraries which need to be loaded
  • An array of the other script libraries to load

I know that there are versions of this out there, but I’m not sure that I’ve seen anything that covers these capabilities well.  If there is, please point me to it!  Otherwise, post your comments about whether the requirements make sense to you and what else you’d like to see.

UPDATE 2010-07-14: Some nice stuff from Einar Stangvik (as usual) here.

Similar Posts

4 Comments

    1. ok, maybe I read this too fast :-)
      If you search the Internet for jQuery CDN failover, you’ll find some suggestions. I’d say that you don’t need an array for jQuery itself, just try the CDN and fallback to local file.

      1. Christophe:

        I was thkning of an array just for flexibility. If it needs to work for two locations, no reason it can’t work for N locations. It also would allow you to have a multiple failovers, perhaps for separate DEV/PROD scenarios and the like.

        And I know I can’t do it in jQuery. ;+) Reminds me of the VCR that came with a tape to show you how to set it up.

        M.

  1. This is going to be a neat solution. I like the idea of arrays of CDN’s b/c in production you can always modify the script to suit your needs (trim the fat, so to speak). It’ll also make the SPServices a bit more robust, by not having to remember if jQuery is loaded or not. Since that’s one of the more common problems you troubleshoot Marc, it’d be nice for this to solve that once SPServices has been called in to the page.

    Stoked about your jQuery class… Look for me to be called “Rookie” a lot more than the XSL course. =)

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.