Please Donate: The Leukemia & Lymphoma Society - Scenic Shore 150 Bike Tour [Donate Here]

Intellisense for jQuery in Visual Studio 2008

February 1st, 2008

These days I write more JavaScript than C#, or at least it seems like it sometime. To make my work a little easier I have started using the JavaScript support in Visual Studio 2008 that provides Intellisense support if you put Xml Doc into your JavaScript. The problem with working with jQuery in Visual Studio 2008 is that it fails due to how the jQuery script is written. For reasons why Intellisense fails, you can read more from Jeff King. Adjusting jQuery to make Visual Studio happy is not really a priority and not even necessary. I can simply reference the real jQuery script from the web pages and within my scripts I can reference a stub for jQuery that is simply a shell with Xml Doc.

[ Update: Hotfix for Visual Studio 2008 to fix jQuery Intellisense ]

I copied the documentation right from the jQuery website and put it all into the following stub named jquery.intellisense.js, shown below:

Then in any script that uses it I can drop in the following comment to reference the stub.

A problem I had with the initial version of this jQuery stub was that I had defined the methods in the jQuery object as static functions. I had the return values defined as the jQuery type, but it was not working when I chained together methods. What I changed to make it work was to define all of the methods within the context of the object prototype where all methods must be defined in order for them to be used within an instance of the object. Now I can do the method chaining with Intellisense support.

An issue that I do not know how to get around is method overloading. In jQuery a method can take various parameters like in C# but there can only be one function name defined at a time, so that seems there can only be one set of Xml Doc for a method. So what I did with this stub was define the most common parameter combinations that would be used.

Using this stub should be useful since I am going to be integrating a lot of ASP.NET AJAX work with jQuery functionality. They compliment each other very well.

Now that I have a working stub I am interested in augmenting it with more stubs to cover the popular jQuery plugins like dimensions and jQuery UI.

27 Responses to “Intellisense for jQuery in Visual Studio 2008”

  1. Dave Says:

    This is really great. I use jQuery with ASP.NET AJAX often myself. Thanks!

  2. Matt Briggs Says:

    I would love a post on integrating JQuery with asp.net ajax.

  3. Brennan Stehling Says:

    @Matt

    How about a full series covering JavaScript in ASP.NET? I have an outline and will be working on the content over the next couple of weeks. You can see the outline at my new blog.

    http://blog.smallsharptools.com/page/Javascript-in-ASPNET.aspx

    Subscribe to my new blog to get updates. I plan to post an announcement for each of the 7 sections as the tutorial is written.

  4. Matt Briggs Says:

    That would be sweet.

    I'm a big fan of JQuery, and I am a fan of how easy it is to do PageMethods and WebMethods in asmx's, but i really hate UpdatePanels, and I don't like adding the overhead of the ScriptManager just to get some js proxies. I've done some minimal playing around trying to get jquery ajax calls to work with asmx's, but I havn't really been successful, and there is next to no documentation on this stuff on the net.

  5. Intellisense for jQuery muligt i VS2008 - Kenneth Auchenberg Says:

    [...] Læs mere her http://brennan.offwhite.net/blog/2008/02/01/intellisense-for-jquery-in-visual-studio-2008/ [...]

  6. » Daily Bits - February 2, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats. Says:

    [...] Intellisense for jQuery in Visual Studio 2008 (Brennan Stehling) [...]

  7. 10 Links Today (2008-02-05) Says:

    [...] Intellisense for jQuery in Visual Studio 2008 [...]

  8. Link List: jQuery, WF, WCF, OOP and Patterns, ASP.NET, Scrum,... - Guru Stop Says:

    [...] Brennan’s Blog » Blog Archive » Intellisense for jQuery in Visual Studio 2008 [...]

  9. lillbra » Blog Archive » links for 2008-02-07 Says:

    [...] Intellisense for jQuery in Visual Studio 2008 - Brennan’s Blog Så fixar du intellisense för jQuery i Visual Studio. (tags: jquery visualstudio vs2008 tips) [...]

  10. Bilal Haidar [MVP, MCT] : Intellisense for JQuery in VS 2008 Says:

    [...] Intellisense for jQuery in Visual Studio 2008 [...]

  11. Daily Find #29 | TechToolBlog Says:

    [...] Intellisense for jQuery in VS 2008 [...]

  12. Bill Says:

    Hello Brennan,
    I am new to jQuery and would like to master it.

    One question about intellisense in ASPX pages, I am adding the "script" reference in the head section, and when I type "$(" I also get an intellisense, but that's it.

    If I want to see a listing of the methods of " $("p"). " I am not able to get the methods after the "."

    Any advice?
    Thanks

  13. Lance Fisher Says:

    Hi Brennan,

    I liked this solution, but with the update to VS 2008, I decided to add the comments to the actual jQuery file itself. As for the overloads, I just mention the usage in the summary. I hope this is useful: http://lancefisher.net/blog/archive/2008/02/12/intellisense-for-jquery-in-visual-studio-2008.aspx

    Lance

  14. Brennan Stehling Says:

    Thanks Lance. I started work on a XML Doc helper that assists with commenting a JavaScript library by comparing the actual script with a stub file. If the function definition does not exist in the stub it will help with adding it to the stub. This way I can keep a lightweight stub for jQuery or whatever library and then compare it new releases using this tool to make sure I add any new functions. I'd like to eventually have something right in VS 2008 that assists with creating a stub for a library, but I am not aware of a way to get into the Intellisense functionality through a VS 2008 Add-in package yet.

  15. James Hart Says:

    Looks like you came to the same conclusion I did - to document JQuery for IntelliSense, you need a stub file to replicate the API. Even with the announcement that VS2008 can now parse JQuery, you still can't annotate all of its methods.

    I've put together a tool that auto-generates a stub file along the same lines as yours by reflecting on the JQuery API and importing doc comments from JQuery's XML API docs. I've linked the generator on the URL above, and blogged about it at http://blogs.ipona.com/james/archive/2008/02/15/JQuery-IntelliSense-in-Visual-Studio-2008.aspx. Be interested to hear what you think...

  16. Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET - ScottGu's Blog Says:

    [...] Using JQuery with VS 2008 JavaScript Intellisense: One of the improvements we shipped in our recent VS 2008 Hotfix Roll-Up last week was to address issues with JavaScript intellisense support for JQuery (another popular AJAX framework).  Brennan Stehling, James Hart, and Lance Fisher have done blog posts recently that discuss how to enable even richer JQuery intellisense inside VS 2008 using intellisense-friendly JQuery libraries that are referenced while coding (and then swapped out for the real library at runtime).  You can read their blog posts about how this works here and here and here. [...]

  17. 2月17日链接篇: ASP.NET, ASP.NET AJAX, Visual Studio, .NET - Joycode@Ab110.com Says:

    [...] Using JQuery with VS 2008 JavaScript Intellisense: 我们在上个星期发布的VS 2008累积热补中做的一个改进是解决了对JQuery的JavaScript intellisense支持问题(JQuery是另一个很流行的AJAX框架)。 Brennan Stehling,James Hart,和 Lance Fisher最近写了几个贴子,讨论如何使用在编写代码时引用的对intellisense友好的JQuery库(在运行时可以替换成真正的库),在VS 2008中促成更丰富的JQuery intellisense。你可以在这里和这里和这里阅读他们相关的博客帖子。 [...]

  18. Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET « .NET Framework tips Says:

    [...] Using JQuery with VS 2008 JavaScript Intellisense: One of the improvements we shipped in our recent VS 2008 Hotfix Roll-Up last week was to address issues with JavaScript intellisense support for JQuery (another popular AJAX framework).  Brennan Stehling, James Hart, and Lance Fisher have done blog posts recently that discuss how to enable even richer JQuery intellisense inside VS 2008 using intellisense-friendly JQuery libraries that are referenced while coding (and then swapped out for the real library at runtime).  You can read their blog posts about how this works here and here and here. [...]

  19. Enlaces 17 de Febrero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET « Thinking in .NET Says:

    [...] Uso de  JQuery con el intellisense de JavaScript en VS 2008: Una de las mejoras del hotfix de VS 2008 de la semana pasada era corregir algunos problemas del soporte del intellisense de JavaScript para JQuery (otro framework muy popular de AJAX). Brennan Stehling, James Hart, y Lance Fisher ha publicado un post sobre cómo habilitar un intellisense más rico de JQuery en VS 2008 usando las librerías de JQuery que están referenciadas mientras escribimos código. Podéis leer los post sobre esos trabajos aquí, aquí y aquí. [...]

  20. Brennan’s Blog » Blog Archive » JavaScript Browser for Visual Studio 2008 Says:

    [...] the JavaScript Browser, the Script Library Definitions will also generate stubs much like the jQuery stub that created to give the developer Intellisense support. I also expect that other formats will be generated as [...]

  21. Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET - Mirrored Blogs Says:

    [...] Using JQuery with VS 2008 JavaScript Intellisense: One of the improvements we shipped in our recent VS 2008 Hotfix Roll-Up last week was to address issues with JavaScript intellisense support for JQuery (another popular AJAX framework).  Brennan Stehling, James Hart, and Lance Fisher have done blog posts recently that discuss how to enable even richer JQuery intellisense inside VS 2008 using intellisense-friendly JQuery libraries that are referenced while coding (and then swapped out for the real library at runtime).  You can read their blog posts about how this works here and here and here. [...]

  22. vFragoso Says:

    Sorry for my bad english..
    Apparently, both, Lance's and Brennan's stubs, works fine, being Lance's more complete (thanks both), but I have noticed a bug, a problem, I don't know.
    When you type your code like this $().css().add().parent(), intellisense works fine. But when you use the width() or height() function, intellisense stops working after the function.
    Something like $().width().INTELLISENSE?? Lol! Weird.
    Hope you all have a solution, I haven't too much knowledge at this to solve myself (yet! ;D). Thanks anyway.

  23. Brennan Stehling Says:

    @vFragoso,

    The reason width and height do that is because they are returning numbers and not a jQuery object. Intellisense is doing what is should here. The fact that it is a number and not a jQuery object prevents you from continuing to chain methods after this point.

  24. vFragoso Says:

    First of all, thanks for replying.
    But, as I intended to say, the jQuery functions width() and height(), can, both, gets or sets the size of the object.
    In case they are getting, it's preferible that intellisense stops after it.
    But in case you are setting the width or height value, it wouldn't, since in jQuery structure and documentation, you are allowed to write $("div.class").height(100).width(100).css("color", "red");

    Another example is:
    The function css("color") can get the current color of the object, and then, intellisense should stops after it.
    But, it can also set the color of the object and it should continue working after it.

    I guess it's better explained now and I'll try to make some suggestions to Jeff King (the releaser of the hotfix), maybe he can help us better with that, since I guess jscript intellisense doesn't offer too much support for ideas like that.

    I also thought if Microsoft adds some way to give multiple descriptions to one function, like:
    1. Gets computed pixel width..
    2. Sets css width to..
    Letting us to chose with those little cool arrows (like in C# intellisense)

    It would be really useful, allowing us to define parameters, or not for each of these definitions.

    Something like:

    Thanks anyway!

  25. vFragoso Says:

    I guess my last example didn't appeared as I intended.

    Here it goes again:

    definition
    summary /
    param /
    /definition

    definition
    summary /
    /definition

  26. Brennan Stehling Says:

    @vFragoso

    Ah, I think this is a consequence of the function overloading. Sometimes it returns a number and sometimes it turns the jQuery instance. With the XML Doc Comments there is no way of indicating (as far as I know) that a function can be overloaded based on the parameters and the type of the parameters.

    Unfortunately I am not sure what Jeff King's team plans to do about the implicit overloading that jQuery and other libraries are doing. I am just glad I do not have to write the code to figure it out. I am just an outsider trying leverage what his team has created. I suspect that in the next major release it will be possible to define multiple XML Doc definitions with a function and Intellisense will show them all just like they do for overloaded methods in C# and VB.

    In the JavaScript Browser I am developing I do allow for multiple definitions for a function. Now I just have to have a way to navigate to the documentation once we can get the context from the editor when the developer is editing a JavaScript file. I suppose I can simply count how many times each function doc is viewed and show the most used one, or somehow detect the parameter count and type and switch the documentation viewer based on those details. Once the JavaScript Browser team (me and a few volunteers) are able to leverage either VS SDK or DXCore to extract this data this will get interesting.

    http://brennan.offwhite.net/blog/2008/02/25/javascript-browser-a-work-in-progress/

  27. Jack Says:

    Thanks for your post. I add the following line inside your comment file:

    jQuery.__typeName = "jQuery";
    jQuery.__class = true;

    Now when you type $ or jQuery, the intellisense shows a nice class icon. Works great!