How you can force the Ajax Script Loader to use jQuery 1.4
AJAX, ASP.NET, JavaScript, jQuery By Dave Ward on January 15th, 2010If you’ve already begun using Microsoft’s new Ajax Script Loader with a CDN-hosted version of jQuery, today’s release of jQuery 1.4 may have left you wondering how to upgrade. Personally, I didn’t want to wait on a new version of Start.js, nor did I want to abandon the script loader now that I’ve become accustomed to its benefits.
No doubt, an upcoming ASP.NET Ajax Library iteration will update Start.js’ jQuery definition to reference jQuery 1.4.x. Regardless, knowing how to patch the script loader on your own terms is something that will be of recurring usefulness.
Luckily, the script loader is open and extensible enough that it’s possible to change which script versions are used. So, I want to briefly show you how the script loader defines JavaScript includes and how you can patch those definitions without modifying Start.js itself.
Using the script loader to inject jQuery
If you aren’t familiar with the script loader itself or with using it to asynchronously request jQuery, I recommend checking out its documentation and examples on the new ASP.NET Ajax Wiki.
To use it most basically, simply include a reference to Start.js either locally or on the Microsoft CDN, and then use this JavaScript to instruct the script loader to inject jQuery into the page:
// Instruct the script loader to request // jQuery in the background. Sys.require(Sys.scripts.jQuery); // This callback runs later, after jQuery // has been asynchronously loaded. Sys.onReady(function () { console.log("Loaded jQuery " + jQuery.fn.jquery); });
Monitoring the request in Firebug illuminates what that Sys.require statement triggered in the background:

As you can see, the end result is pretty straightforward. The primary benefit of this asynchronous loading technique is that scripts loaded through the script loader are non-blocking and may be loaded in parallel. By contrast, JavaScript includes referenced through HTML script elements block all rendering and further requests until they are parsed and executed. The difference is often significant.
Now, if only it were injecting jQuery 1.4 instead of 1.3.2.
Understanding how Sys.scripts.jQuery is defined
The Sys.scripts.jQuery parameter to the script loader is actually just a JavaScript object that defines a few parameters about the include. Here is how that is defined in Start.js (edited slightly for readability here):
var path = (window.location.protocol === "https" ? "https" : "http") + "://ajax.microsoft.com/ajax/"; loader.defineScripts(null, [ { name: "jQuery", releaseUrl: path + "jquery/jquery-1.3.2.min.js", debugUrl: path + "jquery/jquery-1.3.2.js", isLoaded: !!window.jQuery } ]);
If you’re using a local copy of Start.js, one option is to modify this jQuery script definition inside Start.js itself. However, I discourage that alternative. Not only would it require constant, manual maintenance after every new release, but it isn’t an option when using the Microsoft Ajax CDN’s copy of Start.js.
Defining a new target for it
Another option is to simply modify the Sys.scripts.jQuery object itself. Letting Start.js initialize the definition with jQuery 1.3.2’s path doesn’t hurt anything as long as we redefine it before calling Sys.require.
Inspecting the Sys.scripts collection in Firebug reveals how straightforward that modification will be:

Thanks to to the power of interactively interrogating the object in Firebug, it becomes clear that updating the releaseUrl and debugUrl properties of that object should be all that is necessary.
Updating the example to use jQuery 1.4
Because jQuery 1.4 isn’t available on the Microsoft CDN yet, I’m going to target Google’s AJAX Libraries CDN for jQuery instead. Once Microsoft’s CDN is updated with jQuery 1.4.x, this approach will work for either CDN.
// These re-definitions must be executed after // Start.js, but before Sys.require. var CDN = "http://ajax.googleapis.com/ajax/libs"; Sys.scripts.jQuery.releaseUrl = CDN + "/jquery/1.4.1/jquery.min.js"; Sys.scripts.jQuery.debugUrl = CDN + "/jquery/1.4.1/jquery.js"; // Instruct the script loader to request // jQuery in the background. Sys.require(Sys.scripts.jQuery); // This callback runs later, after jQuery // has been asynchronously loaded. Sys.onReady(function () { console.log"Loaded jQuery " + jQuery.fn.jquery); });
That’s all there is to it. Reviewing the requests in Firebug again, you can see that the same Sys.require statement is indeed now loading jQuery 1.4 from Google’s CDN:

Bonus: jQuery Validate 1.6
As I write this, Start.js’ definition for Sys.scripts.jQueryValidate is still targeting version 1.5.5 of the plugin, but 1.6 is the current version.
Using what’s been covered in this post, pointing the script loader at the newest version of jQuery Validate is no problem:
var MSCDN = "http://ajax.microsoft.com/ajax"; var GoogCDN = "http://ajax.googleapis.com/ajax/libs"; // Please forgive my ugly formatting to make this fit. Do not try // this at home (unless your editor is only 492px too). Sys.scripts.jQuery.releaseUrl = GoogCDN + "/jquery/1.4.1/jquery.min.js"; Sys.scripts.jQuery.debugUrl = GoogCDN + "/jquery/1.4.1/jquery.js"; Sys.scripts.jQueryValidate.releaseUrl = MSCDN + "/jQuery.Validate/1.6/jQuery.Validate.min.js"; Sys.scripts.jQueryValidate.debugUrl = MSCDN + "/jQuery.Validate/1.6/jQuery.Validate.js"; // Instruct the script loader to request // jQuery and jQuery Validate in the background. Sys.require([Sys.scripts.jQuery, Sys.scripts.jQueryValidate]); // This callback runs later, after both jQuery // and jQuery Validate have been loaded. Sys.onReady(function () { console.log("Loaded jQuery " + jQuery.fn.jquery); });
The jQuery Validate situation is a good example of why knowing how to update these script definitions yourself is worthwhile. Because the script loader was designed to be so flexible, there’s no need to wait on a new release of Start.js or give up its benefits.
Similar posts
What do you think? Your comments are welcome.
I appreciate all of your comments, questions, and other feedback, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums instead.
If you're replying to an existing comment, please use the threading feature. To do this, click the "Reply to this" link underneath the comment you're replying to.
5 Mentions Elsewhere
- uberVU - social comments
- Tweets that mention How you can force the Ajax Script Loader to use jQuery 1.4 | Encosia -- Topsy.com
- tripwire magazine | tripwire magazine
- jQuery 1.4 Released: Sneak Peek on New Features and Enhancements | Afif Fattouh - Web Specialist
- 網站製作學習誌 » [Web] 連結分享


Looks like one of your examples “Updating the example to use jQuery 1.4″ has an extra ‘/’ in the URL you assign to CDN. Other than that thanks for the article. :)
Thanks; you’re right. Updated it to fix that.
What about using this with HTTPS? The scriptloader automatically switches the link between HTTP and HTTPS for jQuery 1.3.2.
You can use window.location.protocol to determine if SSL was used to request the page and then “patch” Sys.scripts.jQuery accordingly. Something like this, for example:
The gotcha with window.location.protocol is that it includes the colon suffix, so expect “http:” or “https:”.
Thanks Dave. Your posts are always concise and right on the money. I love your blog.
Thanks
Thanks for another useful post. I changed the start.js while working, but I liked your last way of coding(using google CDN). Keep up the good work and make our life easy with post like this:).Thanks again.
Thanks for another very useful article.
I’ve just started to see the light with the ajax client script loader and there are obvious benefits.
Three questions:
1. Does the script loader consider locally cached versions of the scripts, i.e. on my PC (C:\Documents and Settings\administrator\Local Settings\Temporary Internet Files)? Would it consider this before downloading from a CDN/local web server?
2. I noticed that pointing start.js at a local web server () pulls in local versions of the Microsoft scripts but uses the CDN URL for jQuery. Why is this? And what is the best way to override? I have an intranet web server that has no internet access and therefore no CDN access, which is a problem if the start.js is hard coded into the 1.3.2 path on a CDN.
3. Is the jQuery ($.getScript…) doing something similar to the loader, i.e. asnychnronous loading of scripts?
1. When the script loader injects a <script> element, the browser can still act on that by loading a cached version if appropriate. That still depends on the expires header, as usual.
2. You can use this same “patching” approach to change the reference to a local location, or edit the script definitions in start.js. Alternatively, you could define a new jQueryLocal script definition with loader.defineScripts().
3. Yes. It’s not as sophisticated (e.g. how would you handle loading two scripts with dependencies using $.getScript), but uses the same type of asynchronous loading approach.
Thanks for your replies to my questions Dave – very useful.
I’m still not clear on why the start.js contains a hard coded reference to ajax.microsoft.com/ajax for jQuery when the other Microsoft libraries react to the location of start.js (local or CDN).
Is this by design?
I agree that it should be consistent. I’m not sure why they chose to always use the CDN.
It seems to me that there are several pathways or approaches to encorporating jQuery into ASP.Net, whether it be MS Ajax with ScriptManager, a simple reference in your WebForm, etc. I’ve read your series for about a year now on different techniques and wondered if you considered putting a road map of the different approaches, and maybe links to the posts respective to each “style”? Maybe a permanent page?
Anyway, I enjoyed the post as always.
Personally, I’ve almost always used script elements. Going forward, I suspect I’ll be using the script loader often.
The patterns and practices “V.Next” web guidance is going to include a document that discusses methods for including scripts, and the sample application makes heavy use of the script loader (which led to discovering and finding solutions for interesting real-world problems around using it).
Do you know what happened to the script loader since the VS2010 release? I know the Ajax Library was merged with the control toolkit but there is no where on the microsoft cdn anymore to get a reference to start.js. I grabbed the toolkit source and it looks like they have a reference to start.js but it is buried in the site demo. I can’t find documentation anywhere. Is Microsoft scrapping this?
There’s a copy of start.js at this location:
http://ajax.microsoft.com/ajax/act/40412/start.js
Long-term, I think they’re focusing on a more generalized version of start.js’ script loader that would work easier in conjunction with jQuery or even be part of jQuery core. So, maybe not scraping it, so much as revamping it.
Thanks for the fast response and the heads up on the future of start.js.
Great post! By the way you can also add some shorcut to “onReady”, check this!