Contents tagged with JavaScript

  • Enabling JavaScript IntelliSense in External Libraries

    To get JavaScript IntelliSense working in VS 2008 SP1, you need to tell IntelliSense the location of the libraries that you're using. You do that by adding a special comment at the top of the .js file. It's three slashes followed by the reference in an XML element syntax.

    For example, my JScript.js file has a dependency on the jQuery library. So, here's what I add to the top of JScript.js:

    /// <reference path="jquery-1.2.6.js" />

    IntelliSense then parses the referenced file. Just start typing and you start seeing the jQuery members:

    IntelliSense for JavaScript

    I'm not sure whether it's required to put the reference pointer at the very top of the page but my IntelliSense failed to work when I included comments *before* the reference. The following didn't work:

    /*-------------------------------------------------------------------- 
    Don't do this!!!!!!!!!!!!
    --------------------------------------------------------------------*/
    /// <reference path="jquery-1.2.6.js" />

    If the developer of a library wants to provide even more info, IntelliSense will pick that up too. Just use the three-slash comments with the following syntax:

    trim: function(text)
    {
        /// <summary>Removes whitespace from the beginning and end</summary>
        /// <param name="text">The string to be trimmed</param>
        /// <returns>string</returns>
        return (text || "").replace(/^\s+|\s+$/g, "");
    },

    Here's how the summary appears in the IDE for the trim() function above:

    Including a Description

    With IntelliSense, JavaScript debugging, and enhanced JavaScript formatting, I may start to hate JavaScript just a little less! <grin>

    Ken

    Read more...

  • Visual Studio 2008 SP1 Adds JavaScript Formatting

    Just a "two thumbs up" for the addition of JavaScript™/JScript® formatting in Visual Studio 2008 Service Pack 1. I don't like leaving messy-looking code - especially since JavaScript makes the trip all the way to the client.

    My formatting preference is to show opening and closing braces on a new line, so here's what you need to do in VS 2008:

    1. From the Tools menu, click Options.
    2. In the Options dialogue box, check Show All Settings in the bottom left corner. (BTW, it makes no sense for Microsoft to hide so many excellent tools.)
    3. Expand the Text Editor node, expand JScript and select Formatting.
    4. In the New Lines group box, check both Place Open Brace On New Line For Functions and Place Open Brace On New Line For Control Blocks.
    5. Click OK to close the Options dialogue box.
    6. In a .js page, press Ctrl+K, Ctrl+D (or, From the Edit menu, click Format Document).

    This is one of those productivity improvements that makes the IDE indispensable.

    Ken

    Read more...