JQuery IntelliSense in Visual Studio 2008

FURTHER UPDATED January 19 2009: This post predates several important developments in the world of JQuery intellisense, not least the release by Microsoft of the ‘official’ JQuery 1.2.6 intellisense file. I’ve also updated my intellisense file for JQuery 1.3.0. More Information

UPDATED July 5 2008: The intellisense file now supports JQuery 1.2.6. More Information

ScottGu recently announced that VS2008 JavaScript IntelliSense no longer fails completely when it runs into JQuery, which is fantastic news - and an impressive testament to the flexibility of VS2008's JavaScript parsing abilities. Previously, merely referencing JQuery was enough to disable all JavaScript IntelliSense in VS2008, which meant that JQuery addicts like me had simply never seen how impressive this system really can be.

But even though they've got over the crashing problem, the basic IntelliSense experience is still a little sparse, especially since JQuery employs minimally descriptive one-or-two character parameter names in an effort to keep source size down. JQuery also frequently employs method chaining (for more on which, see blogs passim) but VS IntelliSense is unable - against raw JQuery - to determine the return type and generate chained method and parameter hints.

So visual Studio IntelliSense starts off promisingly:

plain.1

but then falls flat as soon as you've hit the dot:

plain.2

This has led to a spate of efforts to annotate JQuery with IntelliSense-friendly documentation comments. Unfortunately, there's a limit to how much you can do with adding annotations directly to the JQuery code, and it's a time-consuming - and manually intensive - operation that will be blown away by the next release of JQuery.

One of the key problems is that JQuery - in classic JavaScript space-conscious style - creates a number of its prototype functions by repeatedly using the same function declaration in a loop, relying on JavaScript closures to make each function behave differently. Here's a sample where JQuery sets up a series of event hook methods:

jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
	"mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + 
	"submit,keydown,keypress,keyup,error").split(","), function(i, name){
	
	// Handle event binding
	jQuery.fn[name] = function(fn){
		return fn ? this.bind(name, fn) : this.trigger(name);
	};
});

Stick a documentation comment in the top of that function, and you'll end up with the same IntelliSense documentation on 20 JQuery methods. Hardly ideal.

There has to be a better way; we use JQuery intensively at work, and so, with the encouragement of my colleague Duncan Smart, I've set out to produce better JQuery IntelliSense, using an automated process rather than manual annotation.

Duncan also pointed me towards the JQuery machine-readable documentation at http://jquery.com/api/. Sadly it's a little out of date, but hopefully it will be updated at some point, and when it is, the automated process should be able to employ the updated data and rebuild the annotated JQuery.

Visual Studio figures out external script references, such as to JQuery, by following special reference comments it finds at the top of .js files:

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

In practice, this means that you can reference any JavaScript file through this mechanism, not just those that you're actually going to run at runtime. Most sites deploy a minified version of JQuery to the browser, but you could reference instead an annotated JQuery file here to get IntelliSense support.

There is a different resolution employed to provide IntelliSense from within <script> tags, where VS attempts to follow <script src=""> links to external includes, and even traces includes it finds in the master page. But we can fool the design-time engine by including <script> tags that won't be output to the browser - for example wrapping the tag inside an <asp:PlaceHolder> with its Visible property set permanently to false. If it overrides the definitions that will really be imported at runtime with its own design-time definitions, we get IntelliSense - in ASPX pages, HTML pages, and so on.

In which case, the question arises, why does the file you're referencing here need to contain anything other than the annotations to drive IntelliSense? Why make VS2008 work so hard to understand JQuery in the first place, when you're already lying to it about which JavaScript source file you're using?

So the approach I've ended up with is not to annotate the JQuery source code, which has the limitations mentioned before, but instead to generate completely different JavaScript that defines a stub that looks exactly the same as JQuery from the point of view of IntelliSense, but which contains no actual functionality. It's a design-time 'header file' that contains all the documentation and structure of JQuery but none of the code. The result is fully-functional method and parameter IntelliSense. Where previously VS drew a blank, now it can continue to help.

intel.1

 intel.2

 intel.3

intel.4 

Employing the script-hiding technique mentioned above, here it is functioning in an ASPX page:

aspx

To produce the script, I turned, naturally, to JavaScript. Using JavaScript, it's possible to examine the actual JQuery API and object model as it exists in the web browser, and construct a script that outputs the same structure. Along the way, I can also generate the appropriate documentation comments from the XML source data at http://jquery.com/api/data/jquery-docs-xml.xml, and include IntelliSense comments. Here's a sample of the output it ends up producing:

jQuery = $ = function (expr, context) {
	/// <summary>
	/// 1: $(expr, context) - This function accepts a string containing a CSS or basic XPath selector which is then used to match a set of elements.
	/// 2: $(html) - Create DOM elements on-the-fly from the provided String of raw HTML.
	/// 3: $(elems) - Wrap jQuery functionality around a single or multiple DOM Element(s).
	/// 4: $(fn) - A shorthand for $(document).
	/// </summary>
	/// <returns type="jQuery"></returns>
	/// <param name="expr" />
	/// 1: expr - An expression to search with
	/// 2: html - A string of HTML to create on the fly.
	/// 3: elems - DOM element(s) to be encapsulated by a jQuery object.
	/// 4: fn - The function to execute when the DOM is ready.
	/// </param>
	/// <param name="context" optional="true" />
	/// 1: context - (optional) A DOM Element, Document or jQuery to use as context
	/// </param>
	/// <field type="String" name="jquery">The current version of jQuery.</field>
	/// <field type="Number" name="length">The number of elements currently matched.</field>
};

You can run the JQuery IntelliSense Header Generator in your browser, copy and paste the resulting script into a .js file, and then you can use it as an IntelliSense source.

We've posted the header generator up here. Or, you can download a copy of the header file from here. Let me know in the comments if you find it useful, or if there's anything you think needs changing about it.

UPDATED: I just noticed that Brennan Stehling has produced something similar to this stub file - apparently manually. Hopefully he won't be too annoyed I've made a tool that does the same thing.

Print | posted on Friday, February 15, 2008 2:59 PM

Comments on this post

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Hi James,

I really like this approach. Commenting the whole library by hand was good for getting familiar with jQuery, but I don't know that I want to spend an afternoon on it everytime that a new version is released.

It would be really nice if Visual Studio directly supported stub-files and overloads. I noticed that you did the same thing that I did with including all the overloads in the summary. It would be nice if you could cycle through them like you can with C# and get hints on the different parameters too.

The only downside I can think of with using a stub is debugging. With the actual file, you can see comments when you step into the source, but really, I don't see this as much of a problem. You can still use the "development" version of jQuery. Brennan mentioned that he was working on an automated tool too. This might make a good project on codeplex. I created one a few days ago at www.codeplex.com/jsintellisense, but I don't know that I'm going to be able to spend much time on it right now.
Left by Lance Fisher on Feb 15, 2008 4:48 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
How do I use this in VS ?

ie. where does it go?
Left by Steve on Feb 16, 2008 3:20 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
@Steve - good question, I guess I didn't make this absolutely clear. You need to place the generated JQuery stub file in your project, alongside your real JQuery javascript file. Then, wherever you reference the JQuery library you need to augment or replace that reference with a reference to the intellisense-commented stub file instead.

So, if you've got a JavaScript .js file that uses JQuery functions, you need to put

/// <reference path="jquery.intellisense.js"/>

as the very first line - this will tell the intellisense system to use the stub file.

In an ASPX page or masterpage where you're currently importing the jquery library with a <script src="jquery.js"> tag, you need to use a technique like the one I mention above of embedding an additional <script src="jquery.intellisense.js"> tag after that tag within an <asp:Placeholder Visible="false">:

<asp:PlaceHolder Runat="server" Visible="false">
<script src="jquery.intellisense.js"></script>
</asp:PlaceHolder>

Hope that clarifies things...
Left by James on Feb 16, 2008 5:33 PM

# Scott Hanselman - ComputerZen

Requesting Gravatar...
Scott Hanselman - ComputerZen
Left by Pingback/TrackBack on Feb 16, 2008 9:06 PM

# Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

Requesting Gravatar...
Here is the latest in my link-listing series .&amp;#160; Also check out my ASP.NET Tips, Tricks and Tutorials
Left by ScottGu's Blog on Feb 17, 2008 7:01 PM

# 2月17日链接篇: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

Requesting Gravatar...
【原文地址】 Feb 17th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET 【原文发表日期】 Sunday, February 17, 2008
Left by Joycode@Ab110.com on Feb 17, 2008 10:10 PM

# Colegamenti del 17 Febbraio: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

Requesting Gravatar...
Colegamenti del 17 Febbraio: ASP.NET, ASP.NET AJAX, Visual Studio, .NET
Left by Scott Guthrie Italian WebLog on Feb 18, 2008 1:18 PM

# Brennan's Blog

Requesting Gravatar...
As I work on JavaScript I often consult with Google to find documentation on which functions are available and how they work. Sometimes I find the answer right away and other times it takes more effort to find the right answers. I really want it to be easier and faster to find the right answers quickly so I started work on a Tool Window that I call JavaScript Browser that integrates within Visual Studio 2008....
Left by Pingback/TrackBack on Feb 18, 2008 8:46 PM

# Enlaces 17 de Febrero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET

Requesting Gravatar...
Enlaces 17 de Febrero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET
Left by Pingback/TrackBack on Feb 19, 2008 5:26 PM

# up-to-date machine-readable jQuery documentation

Requesting Gravatar...
you can get "jQuery machine-readable documentation" for any jQuery version with the wikiapi2xml tool: http://dev.jquery.com/browser/trunk/tools/wikiapi2xml. the .py scripts produce the same xml as the out of date http://jquery.com/api/ . they are supposed to run cgi, so you have to fiddle a bit to get them run command line. or just google for "createjQueryXMLDocs.py" and you may find a running cgi version which spares you from this (http://www.exfer.net/jquery/createjQueryXMLDocs.py). you could even modify this script to generate your stub file. or incorporate it in your script ...

that other tool in the tools dir also looks interesting: http://dev.jquery.com/view/trunk/tools/api-browser/.

thanks for the nice work!
Left by Axel Kollmorgen on Feb 21, 2008 10:45 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
@Axel - thanks for the pointers. I've had a play with that python script and trying to get it running under IronPython in a simulated CGI environment (you can run any CGI program from the command line if you just set the rgith environment variables).

If nothing else, it's been a good excuse for playing with IronPython (I have managed to run it successfully under native Python, but I love a challenge).

It does generate output in a slightly different format than the file I worked from (I think I was using the output from createjQueryXMLDocsOldFormat.py) but it's definitely what we're looking for.

Left by James on Feb 22, 2008 2:47 PM

# (在Visual Studio 2008中让jQuery支持智能感知)

Requesting Gravatar...
(在Visual Studio 2008中让jQuery支持智能感知)
Left by Pingback/TrackBack on Feb 22, 2008 2:49 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
yes, the output from both createjQueryXMLDocs.py and createjQueryXMLDocsOldFormat.py is different to the old 1.1.2 API xml docs, the one from createjQueryXMLDocs.py more than the one from createjQueryXMLDocsOldFormat.py. i noticed when i tried to generate the stub file for jquery 1.2.3 with your script. but yes, it should be possible to adapt that script to generate a complete jquery 1.2.3 documentation stub.

another thought: as the input is xml, it might be easier to generate the output / the documentation stub with xsl. gonna give it a try.
Left by Axel Kollmorgen on Feb 23, 2008 12:27 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
@Axel - indeed, if the only data I was using was the XML documentation, XSL would be a great choice; but the goal of the JavaScript tool is to examine the complete structure of a live JQuery object using JS property reflection, and generate its stub file based on that, annotated with appropriate documentation comments.

For instance, based on the documentation alone, you can determine that there is a property called '$.browser'; but you can't determine that there's a '$.browser.msie' member on it.

By reflecting over a live JQuery instance, the JavaScript code finds all the properties that actually exist and simulates them, which is why I ended up with this approach.
Left by James on Feb 25, 2008 12:43 PM

# jQuery Intellisense in Visual Studio

Requesting Gravatar...
jQuery Intellisense in Visual Studio
Left by Pingback/TrackBack on Feb 25, 2008 4:58 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
jQuery.browser is documented quite thoroughly in the api wiki and the xml docs based on it. it mentions all the members / available flags, incl. 'msie'. it also mentions that jQuery.browser.version, which is a member of jQuery.browser, too, is *not* a browser flag but something different. so it actually knows *more* than pure introspection with a live jquery instance. judging from this, i think that the api xml doc should be all you need for generating a complete vstudio doc stub, without the need for additional jquery introspection.

i won't find time for testing this, though. not soon, anyway.
Left by Axel Kollmorgen on Feb 26, 2008 7:08 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
@Axel - indeed, the documentation 'mentions' all the properties, as part of the descriptive text and examples. But a 'mention' isn't especially easy to parse out of the documentation, and the XML files I'm looking at don't have specific elements corresponding to those subproperties at all.
Left by James on Feb 26, 2008 1:32 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
I like you're automated version, but it's a little out of date to be usable. 1.2 introduced lots of changes. Maybe they will update that documentation
Left by Chris on Feb 29, 2008 9:03 PM

# Вчера пытался заставить заработать intellisense в VS2008 для jQuery...

Requesting Gravatar...
Вчера пытался заставить заработать intellisense в VS2008 для jQuery...
Left by Pingback/TrackBack on Mar 10, 2008 1:32 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Excellent work. I am using it for jQuery 1.2.3. Thanks!
Left by Andrei on Mar 11, 2008 3:09 PM

# The Agileer: ASP.NET MVC Framework, Ajax, & jQuery

Requesting Gravatar...
... In order to have some useful intellisense we add a second reference to a "dummy" jQuery script, which contains only the information required for jQuery intellisense, basically empty shells with XML documentation comments. You can get the jQuery intellisense script from here....
Left by Pingback/TrackBack on Mar 17, 2008 10:16 PM

# Nabble: jQuery intellisense in VS2008 (including chaining)

Requesting Gravatar...
At first, intellisense doesn’t work on chained methods, but James Hart figured out a way to solve that: He wrote an automated script that extracts the jQuery XML documentation into a js file, which is used by VS in the intellisense, showing methods, parameters, etc. Unfortunately, the XML documentation he had was pretty old (1.1.2)

I used the excellent python script by David Serduke (http://www.exfer.net/jquery/createjQueryXMLDocs.py) to extract the latest documentation straight out of the jQuery wiki and export it as XML, then updated James’ script to work with the new XML schema and re-generated the file.
Left by Pingback/TrackBack on Mar 17, 2008 10:20 PM

# fordie's blog

Requesting Gravatar...
One thing that was missing until now though was a way of getting jQuery intellisense in VS2008. Yesterday I spotted this article by James Hart which describes how this missing functionality can be be added.
Left by Pingback/TrackBack on Mar 20, 2008 5:45 PM

# JQuery IntelliSense in VS2008

Requesting Gravatar...
I know, I know, this has been done before. The reason I'm writing this post, however, is to improve on
Left by BradVin's .Net Blog on Apr 28, 2008 7:50 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Thank you. Still no luck?

I get an error that 'Element reference is not supported' and I do not get any intellisense

I'm running VS 2008 with the fix. I have the following on my ms mvc site master:

/// <reference path="/BistroWeb/Content/Scripts/jQuery.intellisense.js" />
<script src="/BistroWeb/Content/scripts/jQuery.intellisense.js" type="text/javascript"></script>

Sorry to be a pain, I'd just like to see this work :) I use jQuery all day :)
Left by Steve on May 13, 2008 1:13 PM

# Visual Studio 2008 Intellisense for jQuery 1.2.5

Requesting Gravatar...
Visual Studio 2008 Intellisense for jQuery 1.2.5
Left by lancefisher.net on May 21, 2008 8:34 PM

# Always 英文技术文章参照( 二 ){ UpdateTime:2008-7-2; } My article in the cnblogs

Requesting Gravatar...
1. ASP.NET Popup Control http://www.codeproject.com/KB/custom-controls/asppopup.aspx 2. Componentart
Left by cnblogs.com on Jul 02, 2008 3:10 AM

# JQuery VS2008 IntelliSense Update

Requesting Gravatar...
JQuery VS2008 IntelliSense Update
Left by James.ToString() on Aug 05, 2008 11:10 AM

# Add jQuery Intellisense To Your Visual Studio

Requesting Gravatar...
Add jQuery Intellisense To Your Visual Studio
Left by Pingback/TrackBack on Aug 05, 2008 11:46 AM

# JQuery IntelliSense in Visual Studio 2008.

Requesting Gravatar...
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Left by DotNetKicks.com on Aug 25, 2008 6:42 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Any way to generate an intellisense file for the jquery ui?
Left by Nate on Aug 25, 2008 6:17 PM

# Google Maps JavaScript Intellisense

Requesting Gravatar...
... solution is pre loader and hand written, rather than the scripted solutions for jQuery (thanks to the way jQuery is documented) ...
Left by Pingback/TrackBack on Sep 08, 2008 12:08 PM

# jQuery: The force be with you!… Mis razones de uso

Requesting Gravatar...
Hace unos cuanto meses estamos utilizando jQuery como nuestro querido framework de Javascript de cabecera
Left by &lt;Jose A. Fernandez /&gt; on Sep 29, 2008 5:20 AM

# 2008-10-02 Whats new in Visual Studio 2008 SP1

Requesting Gravatar...
Presentation: What's new for Presentation Presenter: Mike Ormond ( http://mikeo.co.uk ) ASP.NET ASP.NET
Left by UK Developer Events - Post Event Resources on Oct 03, 2008 10:15 AM

#

Requesting Gravatar...
#think.in Weekly Info-Dose #1 (29th Nov - 3rd Oct 2008)
Left by #.think.in on Oct 05, 2008 11:19 PM

# 2008-10-07 Whats new in Visual Studio 2008 SP1

Requesting Gravatar...
Presentation: What's new for Presentation Presenter: Mike Taulty http://mtaulty.com ASP.NET ASP.NET website
Left by UK Developer Events - Post Event Resources on Oct 08, 2008 6:08 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
James, this is fantastic. Very useful. Thanks so much for making this available to the community.
Left by Herb Caudill on Oct 14, 2008 2:18 PM

# JavaScript IDEs

Requesting Gravatar...
JavaScript IDEs
Left by Pingback/TrackBack on Oct 22, 2008 5:15 PM

# Rich IntelliSense for jQuery

Requesting Gravatar...
A while back we updated IntelliSense to not fail when referencing jQuery. However, getting IntelliSense
Left by Visual Web Developer Team Blog on Oct 29, 2008 2:27 AM

#

Requesting Gravatar...
Eliminating Postbacks: Setting Up jQuery On ASP.NET Web Forms Managing Data On The Client
Left by System.Reflection.Emit on Nov 01, 2008 11:29 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Have you ever looked at visualjquery.com documentation? If it is more up to date (which I think it is), then parsing through that would be better. He has a js file that is annotated (not in the Visual Studio way) that he uses to show the documentation. I feel some code tinkering coming on this evening.... :)
Left by A'braham Barakhyahu on Dec 09, 2008 9:02 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Nice. I must learn more about jquery
Left by radio on Mar 16, 2009 2:12 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Well, it works with HTML, but not XHTML. Valid XHTML uses CDATA sections inside script tags, which stop JavaScript intellisense from working.

Of course, this isn't a problem for most Microsofties, who don't care about valid HTML or XHTML, and still use language="JavaScript" in script tags as if it's 1998.
Left by Matt on Apr 30, 2009 3:28 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
hi,

do we really required VS 2008 sp1 to use jquery intellisense. or it can work without that too.
Left by kk on Aug 15, 2009 9:08 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Why was the decision made to put the { on the same line with the function header, for loop, etc.

example:
function MyFunct() {
for (int i = 0; i < 20; i++) {
// do something...
}
}

I find this much more readable:
function MyFunct()
{
for (int i = 0; i < 20; i++)
{
// do something...
}
}

It makes it easier to match the { with it's corresponding }.
Was this done out of tradition? If so then the tradition is WRONG! Readability should always be a primary consideration. I scan code very rapidly for bugs, techniques, etc. and this slows me down.

I'm currently trying to figure out how to modify the template so that it puts the { on it's own line. It is especially annoying that the IDE keeps moving it back up to the line above whenever I modify it!

If someone has already done this please send me the info on how to do it so I don't have to waste anymore time on this formatting issue.

Thanks!
Left by John McPherson on Dec 30, 2009 4:56 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
This is because , VS will start updating its intellisense and intellisense cache, the first time you reference a script file. So you will see intellisense for only included script sources. You will not get intellisense for any file which is not included or referenced. Check this line in the blog"When you do this VS will now look for a -vsdoc.js file in the same directory as the script file you are referencing, and if found will use it for help and intellisense."
Left by strategic ways to play blackjack on Jan 13, 2010 9:50 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
The remote controls have now made it easier for audiences to tune out commercial videos simply by allowing them to turn down the volume or even switch channels when the advertisement comes on.
Left by video commercial on Feb 23, 2010 3:59 AM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
I love jQuery and ASP.NET ;)
Left by Motoman on Mar 03, 2010 12:57 PM

# re: JQuery IntelliSense in Visual Studio 2008

Requesting Gravatar...
Vedios may take on lives of their own,spawning gags or riffs that may appear in other forms of media such as comedy movies or television variety shows.
Left by video commercial on Mar 09, 2010 8:51 PM

Your comment:

 (will show your gravatar)
 
Please add 5 and 2 and type the answer here: