Home / AJAX

HOW TO Choose Local or Remote Scripts

Modified on 2009/12/04 17:22 by Stephen Walther Categorized as Uncategorized
The Script Loader provides out-of-the-box support for loading ASP.NET Ajax Library or jQuery scripts as well as custom scripts needed by an application. A script named Start.js defines the base Script Loader functionality and can be loaded from a local server or from the Microsoft Ajax Content Delivery Network (CDN). The following code sample demonstrates how to load Start.js locally and from the Microsoft Ajax CDN:
<script type="text/javascript" src="/Scripts/Start.js"></script>

<!-- Loading Start.js from the Microsoft Ajax CDN --> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/beta/0911/Start.js"></script>

When Start.js is loaded from a local path it looks for ASP.NET Ajax Library scripts in that same local path. When it's loaded from the Microsoft Ajax CDN it automatically retrieves ASP.NET Ajax Library scripts from the CDN allowing network performance and caching benefits to be leveraged. The location where scripts are loaded from is controlled by definitions within Start.js such as the one shown next for MicrosoftAjax.js:

{ name: "MicrosoftAjax", releaseUrl: "%/MicrosoftAjax.js", debugUrl: "%/MicrosoftAjax.debug.js", executionDependencies: null, contains: ["Core", "ComponentModel", "History", "Serialization", "Network", "WebServices", "Globalization"] }

Note that the releaseUrl and debugUrl properties in the code sample both start with the % character. This special character is used to define that scripts are relative to the location of Start.js. When Sys.require is used to load jQuery or jQuery Validate scripts they are automatically loaded from the Microsoft Ajax CDN regardless of where Start.js was loaded.

To override the base location used for Microsoft Ajax Scripts the Sys.loader.basePath property can be set to a custom value:
<script src="/Scripts/Start.js" type="text/javascript"></script>

<script type="text/javascript">

Sys.loader.basePath = "../CustomScriptLocation/"; Sys.require([Sys.components.dataView]);


This example tells the Script Loader to look up one level in a folder named CustomScriptLocation for the scripts required by the DataView component (see Figure 1).


Figure 1. Sys.loader.basePath can be used to override the default location where the Script Loader looks for ASP.NET Ajax Library scripts.

In situations where jQuery must be loaded from a local path the Sys.scripts.jQuery.releaseUrl and Sys.scripts.jQuery.debugUrl properties can be modified as shown next:
<script src="/Scripts/Start.js" type="text/javascript"></script>

<script type="text/javascript"> Sys.scripts.jQuery.releaseUrl = "../CustomScriptLocation/jquery-1.3.2.min.js"; Sys.scripts.jQuery.debugUrl = "../CustomScriptLocation/jquery-1.3.2.js "; Sys.loader.basePath = "../CustomScriptLocation/";

Sys.require([Sys.components.dataView, Sys.scripts.jQuery]);


  Name Size
- Sys_loader_basePath.jpg 19.44 KB