Udostępnij za pośrednictwem


JScript IntelliSense Overview

IntelliSense for Microsoft JScript makes it easy to access client-script programming elements in the code editor in Visual Studio or Microsoft Visual Web Developer Express. IntelliSense offers context-appropriate code choices as you type client-script code, such as properties, functions, and objects. You do not have to leave the code editor to search for syntax or parameter documentation. Instead, you can stay in the code editor, find the information that you need, and insert language elements directly into your code. IntelliSense for JScript works much like IntelliSense for managed-code languages.

This topic contains the following sections:

  • Background

  • JScript IntelliSense Features

  • Understanding JScript IntelliSense Errors

  • Additional Resources

For more information about the IntelliSense functionality of Visual Studio, see Using IntelliSense.

Background

IntelliSense helps you write code faster and with fewer errors by providing information while you code. As you work with client script, IntelliSense lists the objects, functions, properties, and parameters that are available based on your current context. You can select a coding option from the pop-up list provided by IntelliSense to complete the code.

IntelliSense makes it easier to complete the following tasks:

  • Find member information.

  • Insert language elements directly into your code.

  • Maintain your context without having to leave the code editor.

  • Support custom IntelliSense with XML code comments.

How JScript IntelliSense Works

JScript IntelliSense provides coding choices based on all script that is relevant to your current programming context. This includes scripting elements in the current file. It also includes any code that is referenced directly or indirectly from your script, such as script file references, assembly script references, service references, and page-associated references.

Determining IntelliSense Context

Your current programming context is created based on the following items:

  • The functions that are defined in the current script block. Inline script blocks are supported in files that have the file-name extensions .aspx., .ascx, .master, .html, and .htm.

  • Each script element that contains a src attribute that points to a script file. The target script file must have the file-name extension .js.

  • JScript files that reference other JScript files by using the references directive.

  • References to XML Web services.

  • The ScriptManager and ScriptManagerProxy controls, if the Web application is an ASP.NET AJAX application.

  • The Microsoft Ajax Library, if you are working in an AJAX-enabled ASP.NET Web application.

    Note

    IntelliSense is not supported for script that is in event-handler attributes on HTML elements, or that is defined in href attributes.

Processing IntelliSense Information

To provide JScript IntelliSense, Visual Studio performs the following operations:

  • Creates a list of dependent JScript file that are based on references in the active document, and based on recursively examining script references in the referenced files.

  • Traverses the list and collects type information and other relevant data from each file.

  • Aggregates the data and passes it to the JScript engine, which makes the type information and data available to IntelliSense.

  • Monitors the files for changes that might affect the IntelliSense list and updates the list as needed.

Back to top

JScript IntelliSense Features

JScript IntelliSense supports the following objects:

  • Document Object Model (DOM) elements

  • Intrinsic objects

  • User-defined variables, functions, and objects

  • External file references

  • XML code comments

  • ASP.NET AJAX objects

HTML DOM Elements

JScript IntelliSense provides programming references for Dynamic HTML (DHTML) DOM elements, such as body, form, and div. Only the elements that are included in the current document and master page are displayed by IntelliSense. JScript IntelliSense also supports the window and document objects and their members.

Intrinsic Objects

JScript IntelliSense provides programming references for the natively intrinsic objects Array, String, Math, Date, and Number. For more information about intrinsic objects, see Intrinsic Objects.

User-defined Variables, Functions, and Objects

When you change a JScript file, Visual Studio scans opened and referenced documents to determine all available code resources. This includes the variables, functions, and objects that you have created. These resources are then available to JScript IntelliSense. 

For more information about user-defined variables, functions, and objects, see Creating Your Own Objects on the MSDN Web site.

External File References

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.

The following example shows markup that uses a ScriptReference object in a ScriptManager control to reference a script file.

<asp:ScriptManager ID="ScriptManager1" runat="server">
  <Scripts>
    <asp:ScriptReference Path="~/Scripts/JScript.js" />
  </Scripts>
</asp:ScriptManager>

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.

The following example shows markup that uses the src attribute in a <script> tag to reference a script file.

<script type="text/javascript" src="~/Scripts/JScript.js">
  
</script>

IntelliSense also supports script files that are embedded as resources in an assembly in ASP.NET AJAX Web applications. For more information about embedded script resources, see Walkthrough: Embedding a JavaScript File as a Resource in an Assembly.

References Directives

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.

XML Code Comments

XML code comments are text descriptions that you add to script. These text descriptions are displayed with IntelliSense when you reference the commented script. XML code comments are available only from referenced files, assemblies, and services.

You can include XML code comments to provide a summary and to provide information about parameters and a function's return value. The following example shows a script that uses XML code comments:

function areaFunction(radiusParam)
{
    /// <summary>Determines the area of a cicle based on a radius parameter.</summary>
    /// <param name="radius" type="Number">The radius of the circle.</param>
    /// <returns type="Number">Returns a number that represents the area.</returns>
    var areaVal;
    areaVal = Math.PI * radiusParam * radiusParam;
    return areaVal;
}

IntelliSense can display code references for XML comments in the following file-reference scenarios:

  • An .aspx file that references a .js file.

  • A .js file that references another .js file.

  • A .js file that references an .aspx file.

IntelliSense is not available when one .aspx file references another .aspx file.

For more information about how to add XML code comments to provide JScript IntelliSense, see Walkthrough: JScript IntelliSense.

ASP.NET AJAX Objects

ASP.NET AJAX also supports JScript IntelliSense. ASP.NET AJAX includes a client framework that extends the standard types that are available in ECMAScript (JavaScript and JScript). To enable JScript IntelliSense to provide details about ASP.NET AJAX objects, XML code comments have been added throughout the Microsoft Ajax Library. These XML code comments are displayed when you use types and members that are contained in the ASP.NET AJAX Library.

Note

Private members are not displayed by JScript IntelliSense. Private members are denoted in ASP.NET AJAX as members that begin with an underscore (_).

Understanding JScript IntelliSense Errors

The JScript IntelliSense engine in Visual Studio automatically provides validation by checking script reference links and pre-processor execution. Script reference link validation is determined based on the current programming context. If a particular reference is not found, Visual Studio provides error information about the reference. Pre-processor error reporting occurs when the execution of dependent files fails.

For more information about how link validation is determined, see Determining IntelliSense Context. For more information about how pre-processor errors are determined, see Processing IntelliSense Information.

Invoking Jscript Validation

In Visual Studio, JScript IntelliSense validation is invoked in two ways:

  • On idle. Validation is invoked automatically on idle in any active document that contains script.

  • Manually. You can also manually invoke JScript IntelliSense validation. To do this, on the Edit menu, click Advanced, and then click Validate Document. This command applies to all open documents.

Validation Results

When Visual Studio cannot resolve script references, or if other types of scripting errors prevent the IntelliSense engine from gathering complete results from the current scripting context, Visual Studio provides feedback in the following ways:

  • Underlined elements in the editor. Wavy red underlines indicate errors. If you hold the mouse pointer over the error, a tooltip displays the error description.

  • Error List window. The Error List window displays the error description, the file where the error occurred, the line and column number, and the project. To display the Error List window, in the View menu, click Error List.

Back to top

Additional Resources

Walkthrough: JScript IntelliSense

How to: Create JScript XML Code Comments

Back to top

See Also

Tasks

Walkthrough: JScript IntelliSense

Dynamically Assigning Script References

Concepts

List Members

Other Resources

Using IntelliSense

About the DHTML Object Model

JScript Objects

SRC Attribute | src Property