BradVin's .Net Blog

Code, snippets, controls, utils, etc. Basically all things .net

(Better) JQuery IntelliSense in VS2008

UPDATE : James Hart has an excellent post about jQuery 1.3 intellisense - you must check it out!

I know, I know, this has been done before. The reason I'm writing this post, however, is to improve on what I have found on the web. For anyone wondering what jQuery is, goto the jQuery site and also read this tutorial. Now onto the good stuff....

If you are like me and you've read the many articles about how to get other javascript libraries to work in VS2008, you'll know that all you really need to do is install the visual studio HOTFIX. This patches your VS and among other things, gets the javascript intellisense working nicely.

Before the hotfix:


After the hotfix:

You'll notice you can now use the jQuery $ shortcut function.

At this point I was delighted. and then I typed $(document). and I got no more intellisense, why???!!! After looking around some more, I found an article on the topic written by Rick Strahl : http://www.west-wind.com/WebLog/posts/251271.aspx. It helped explain things quite a bit and I recommend you read it. Basically, if you use the 'new' keyword in declaring jQuery objects, you get intellisense, so the following works nicely and you get intellisense:

var doc = new jQuery(document);
doc.ready(function() {
var div = new jQuery("#someDiv");
div.html("some text here!");
div.fadeOut(5000);
});

But I still wasn't satisfied. Why should I change the way I write my jQuery just so VS understands it? Yes I get intellisense, but I'm writing more code, and it's unnecessary code at that. Remember : jQuery is the "write less, do more, JavaScript library".

Then onto another point: what about one of the most powerful aspects of the jQuery architecture - the chainability? The following code

$("a").addClass("test").show().html("foo");
doesnt give you any intellisense whatsoever. In order to get intellisense, it has to be written as follows:
var a = new jQuery("a");
a.addClass("test");
a.show();
a.html("foo");

So intellisense for jQuery at this point is actually quite useless I thought. Because in order to use it, we have to write double the amount of code, and jQuery's chainability power isnt being utilised at all. :(
Another bummer was the fact that the method info for the jQuery functions was seriously lacking. Obviously this is because the jQuery library has been written to be as small as possible. But it would be nice if the functions were explained together with the paramaters (exactly like we are used to when writing c# code).

so back to google and back to searching for alternatives...
This is when I found this gem of an article written by James Hart : http://blogs.ipona.com/james/archive/2008/02/15/JQuery-IntelliSense-in-Visual-Studio-2008.aspx .  And it literally solved all my problems. Here is what the method info was before:

And this is how it looks afterwards:

Big improvement! And here is the intellisense in action (using chainability):

Awesome!!!! But how does it work? Basically, a new javascript is generated and referenced from the page. This new file is just a list of methods with no functionality and it 'tricks' VS into producing nice intellisense. It also uses XML documentation to provide useful parameter listings and return types. (You can read more about javascript documentation comments here: http://weblogs.asp.net/bleroy/archive/2007/04/23/the-format-for-javascript-doc-comments.aspx ). For ASPX pages, all you need to do is include the script inside an invisible placeholder :

<script type="text/javascript" src="jQuery.js" ></script>
<asp:PlaceHolder runat="server" visible="false">
<script type="text/javascript" src="JQuery.Intellisense.js" ></script>
</asp:PlaceHolder>
This is to stop the file from actually being included in the rendered page. Note that it must also be after the reference to the 'real' jQuery file.
And in other external .JS files that use jQuery code, add the following to the top of the file:
/// <reference path="jquery.intellisense.js"/>

So now I can write my jQuery code as I did before and I have excellent method information and intellisense. This works fine in external javascript files, plain ASPX pages, master pages, content pages and user controls.

So are there still some issues? Yes there are. Firstly the generated file is using an outdated jQuery version 1.2.1 and even more outdated jQuery documentation version 1.1.2. This is by no means the fault of the author, but rather the jQuery team who have not updated the XML documentation. Secondly, certain functions in jQuery have optional parameters and return different types based on those parameters e.g. the html() function. These functions unfortunately don't give me intellisense because the generator gives them a return type of "Object" in the XML comments. No problem, just edit the generated file and change it where it has <returns type="Object"></returns> to <returns type="jQuery"></returns>
A list of the functions I found that need changing are :

  • css
  • html
  • attr
  • text
  • val
  • height
  • width

Ok, so stricly speaking, it's not correct, because these functions will return a non jQuery object when nothing is passed in, but what the hell. I still get more intellisense than before. And besides, you must at least have some idea of what the functions do, and not rely on intellisense exclusively to write code ;)

You can download my JQuery.Intellisense file with the changes already made. It is using API version: 1.2.3 and Documentation version: 1.2.2.

jQuery Intellisense In 3 Easy Steps

  1. Install VS2008 hotfix
  2. Generate a JQuery.Intellisense.js stub file. Either goto http://www.infobasis.com/sandpit/jQuery-Intellisense/ and generate it, or download a more up to date one (including minor fixes) from here. save it to the same folder as your jQuery file
  3. Add a invisible placeholder into your header and reference the new .js file:
        <asp:PlaceHolder runat="server" visible="false">
            <script type="text/javascript" src="JQuery.Intellisense.js" ></script>
        </asp:PlaceHolder>
    or add a referenece at the top of your .js file
    /// <reference path="jquery.intellisense.js"/>

Download the whole website: JqueryIntellisenseWebsite.zip

Code samples:

simple jQuery page with full intellisense:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>I have intellisense!</title>
<script type="text/javascript" src="jQuery.js" ></script>
<asp:PlaceHolder runat="server" visible="false">
<script type="text/javascript" src="JQuery.Intellisense.js" ></script>
</asp:PlaceHolder>

<script type="text/javascript">
$(function() {
$("#someDiv").hide().html("Some text here!!!").addClass("bold").fadeIn(1000).fadeOut(2000);
});
</script>

<style type="text/css">
.bold{ font-weight:bold; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="someDiv">
</div>
</form>
</body>
</html>


simple master page :
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" src="jQuery.js" ></script>
<asp:PlaceHolder runat="server" visible="false">
<script type="text/javascript" src="JQuery.Intellisense.js" ></script>
</asp:PlaceHolder>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>


simple content page :
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled 

Page" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
$(function() {
$("#someDiv").addClass("bold").fadeIn(2000).fadeOut(1000).html("gone");
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>


simple user control :
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestControl.ascx.cs" Inherits="TestControl" %>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" visible="false">
<script type="text/javascript" src="JQuery.Intellisense.js" ></script>
</asp:PlaceHolder>
<script type="text/javascript">
$(function() {
$("#someDiv").hide().html("Some text here!!!").addClass("bold").fadeIn(1000).fadeOut(2000);
});
</script>
Published Monday, April 28, 2008 7:02 PM by bradvin
Filed under: , , ,

Comments

# April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight@ Tuesday, April 29, 2008 2:12 AM

Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

by Blogs

# Links@ Tuesday, April 29, 2008 5:26 AM

Links

# (Better) JQuery IntelliSense in VS2008@ Tuesday, April 29, 2008 8:40 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: (Better) JQuery IntelliSense in VS2008@ Tuesday, April 29, 2008 2:52 PM

Intellisense + jquery = awesomeness!

# re: (Better) JQuery IntelliSense in VS2008@ Wednesday, April 30, 2008 8:09 AM

Thanks for putting this intellisense info together into one article. And especially like the simple 3 Easy Steps at the end. I'll be updating my Master Pages tonight :)

by Charles

# 28ste April links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight @ Wednesday, April 30, 2008 11:14 AM

Hier vind je mijn laatste link-lijst series . Bekijk ook mijn ASP.NET Tips, Tricks and Tutorials pagina

# re: (Better) JQuery IntelliSense in VS2008@ Thursday, May 01, 2008 3:25 PM

You are the man!!!! Thanks!

by Mitch

# Enlaces de Abril: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight &laquo; Thinking in .NET@ Friday, May 02, 2008 7:08 AM

Pingback from  Enlaces de Abril: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight &laquo; Thinking in .NET

# Weekly Web Nuggets #10@ Friday, May 02, 2008 12:46 PM

Milestone 1 accomplished - 10 weeks of web nuggety goodness! General The Weekly Source Code - OpenID Edition : Familiar with OpenID ? No? Then Scott Hanselman has a must-read post! Why .Net IEnumerable Predicates are Cool : A nice addition for your toolbox

# Links do dia 28 de Abril: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight@ Friday, May 02, 2008 6:11 PM

Aqui está a última versão da minha série de listas de links . Verifique também minha página de Dicas

# jQuery&#8230; ein paar Links | Code-Inside Blog@ Monday, May 05, 2008 2:15 PM

Pingback from  jQuery&#8230; ein paar Links | Code-Inside Blog

# jQuery&#8230; some links | Code-Inside Blog International@ Monday, May 05, 2008 2:27 PM

Pingback from  jQuery&#8230; some links | Code-Inside Blog International

# links for 2008-05-06 - The Boltzmann Constant@ Tuesday, May 06, 2008 10:34 AM

Pingback from  links for 2008-05-06 - The Boltzmann Constant

# re: (Better) JQuery IntelliSense in VS2008@ Tuesday, June 24, 2008 4:32 PM

Thanks, much appreciated. BTW, do you have header file for jQuery 1.2.6?

# re: (Better) JQuery IntelliSense in VS2008@ Tuesday, June 24, 2008 5:14 PM

I don't know if you heard about this or not, but there is a python script (www.exfer.net/.../createjQueryXMLDocs.py) that converts the Jquery wiki docs into xml documentation. For example for the 1.2.6 api go here: www.exfer.net/.../createjQueryXMLDocs.py?version=1.2.6

by ariel

# vs2008@ Thursday, July 10, 2008 8:51 PM

Pingback from  vs2008

by vs2008

# jQuery Intellisense@ Friday, August 01, 2008 3:25 AM

Nachdem ich mich jetzt seit einer Woche mit jQuery beschäftige, möchte ich eigentlich nicht mehr ohne

# re: (Better) JQuery IntelliSense in VS2008@ Saturday, September 20, 2008 7:46 AM

Thanks for clearing up the confusion. Was very easy to implement.

by Riyaz

# jQuery: The force be with you!… Mis razones de uso@ Monday, September 29, 2008 12:20 AM

Hace unos cuanto meses estamos utilizando jQuery como nuestro querido framework de Javascript de cabecera

by

# Visual Studio i??in JQuery IntelliSense@ Tuesday, October 14, 2008 4:13 AM

Pingback from  Visual Studio i??in JQuery IntelliSense

# Good links: ASP .NET and JQuery@ Wednesday, October 15, 2008 12:52 PM

Found a great bunch of links about using JQuery with asp .net Using jQuery to directly call ASP.NET AJAX

# (Better) JQuery IntelliSense in VS2008@ Thursday, November 20, 2008 11:29 AM

Your Story is Submitted - Trackback from DotNetShoutout

# April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight &laquo; .NET Framework tips@ Tuesday, December 02, 2008 5:03 AM

Pingback from  April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight &laquo; .NET Framework tips

# re: (Better) JQuery IntelliSense in VS2008@ Wednesday, December 03, 2008 7:36 PM

Thanks,

you're awesome

by atarikg

# re: (Better) JQuery IntelliSense in VS2008@ Saturday, December 13, 2008 6:29 PM

I've been using the following version of jQuery for development: <a href="jqueryjs.googlecode.com/.../a>

contains detailed comment, supports chaining, etc.  I'm sure people are aware, just putting it out there for those that don't already know.

by Victor

# re: (Better) JQuery IntelliSense in VS2008@ Monday, January 12, 2009 1:27 PM

Great. This is very helpful. The only problem I noticed was that VisualStudio complained: "Element PlaceHolder is not a known element." I changed asp:PlaceHolder to asp:Label and it removed the warning. I also had to compile the project to get IntelliSense appear. Thanks again.

# Place Holder como contenedor para controles din?micos | hilpers@ Saturday, January 17, 2009 3:08 PM

Pingback from  Place Holder como contenedor para controles din?micos | hilpers

# re: (Better) JQuery IntelliSense in VS2008@ Friday, January 23, 2009 3:09 AM

Great Post!

Did you know google are hosting JQuery amongst other JS libraries which you can access using the google AJAX Libraries API:

<a hredf="web2asp.net/.../a>

# Welches Tabregister Control benutzt ihr? | hilpers@ Wednesday, June 03, 2009 4:06 AM

Pingback from  Welches Tabregister Control benutzt ihr? | hilpers

Leave a Comment

(required) 
(required) 
(optional)
(required)