Instead of writing all client script in a page, you can reference external files that include scripting code. This makes it easier for you to reuse code between pages, and it enables client script to be cached by the browser.
If you are working with an ASP.NET AJAX-enabled Web page, you can reference script files by using the ScriptReference object of the ScriptManager control.
If you are not working with an ASP.NET AJAX-enabled Web page, you can reference an external script file by using the src attribute in the opening tag of a script element. The src attribute specifies the URL to an external file that contains the source code or data.
A references directive enables Visual Studio to establish a relationship between the script you are currently editing and other scripts. The references directive lets you include a script file in the scripting context of the current script file. This enables IntelliSense to reference externally defined functions, types, and fields as you code.
You create a references directive in the form of an XML comment. The directive must be declared earlier in the file than any script. A reference directive can include a disk-based script reference, an assembly-based script reference, a service-based script reference, or a page-based script reference.
The following example shows examples of using disk-based reference directives.
/// <reference path="ScriptFile1.js" />
/// <reference path="Scripts/ScriptFile2.js" />
/// <reference path="../ScriptFile3.js" />
/// <reference path="~/Scripts/ScriptFile4.js" />
The following example shows how to create a reference to an assembly-based script.
/// <reference name "Ajax.js" assembly="System.Web.Extensions, ..." />
The following example shows how to reference service-based script:
/// <reference path="MyService.asmx" />
/// <reference path="Services/MyService.asmx" />
/// <reference path="../MyService.asmx" />
/// <reference path="~/Services/MyService.asmx" />
Note: |
|---|
JScript IntelliSense is not supported for script that is contained in Web service (.asmx) files in Web Application Projects (WAP). |
The following example shows how to reference page-based script.
/// <reference path="Default.aspx" />
/// <reference path="Admin/Default.aspx" />
/// <reference path="../Default.aspx" />
/// <reference path="~/Admin/Default.aspx" />
The following rules apply to a reference directive.
The reference XML comment must be declared before any script.
You must use XML comments syntax with three slashes. References made by using standard comments syntax (two slashes) are ignored.
Only one file or resource can be specified per directive.
Multiple references to page-based scripts are not allowed.
If a page reference is specified, no other type of reference directives is allowed.
File names use relative paths. You can use the tilde operator (~) to make application-root-relative paths.
Absolute paths are ignored.
References to files outside the current project are ignored.
Reference directives in referenced pages will not be processed—that is, reference directives are not resolved recursively for pages. Only script that is referenced directly by the page is included.