<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tales from the Evil Empire : JavaScript</title><link>http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx</link><description>Tags: JavaScript</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Orchard team looking for a new developer</title><link>http://weblogs.asp.net/bleroy/archive/2009/11/23/orchard-team-looking-for-a-new-developer.aspx</link><pubDate>Mon, 23 Nov 2009 19:24:19 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7263678</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7263678</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/11/23/orchard-team-looking-for-a-new-developer.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/Dragonfly_5E7683B7.jpg" width="244" height="164" /&gt; My team is looking for a new full-time developer. The project is to build a completely new open-source CMS based on ASP.NET MVC 2. It’s a lot of fun :)&lt;/p&gt;  &lt;p&gt;&lt;a href="https://careers.microsoft.com/JobDetails.aspx?ss=&amp;amp;pg=0&amp;amp;so=&amp;amp;rw=1&amp;amp;jid=9434&amp;amp;jlang=EN"&gt;https://careers.microsoft.com/JobDetails.aspx?ss=&amp;amp;pg=0&amp;amp;so=&amp;amp;rw=1&amp;amp;jid=9434&amp;amp;jlang=EN&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7263678" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CSS/default.aspx">CSS</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Enabling the ASP.NET Ajax script loader for your own scripts</title><link>http://weblogs.asp.net/bleroy/archive/2009/11/23/enabling-the-asp-net-ajax-script-loader-for-your-own-scripts.aspx</link><pubDate>Mon, 23 Nov 2009 15:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7262909</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7262909</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/11/23/enabling-the-asp-net-ajax-script-loader-for-your-own-scripts.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2009 Bertrand Le Roy" border="0" alt="(c) 2009 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/IMG_2351_078C57E8.jpg" width="244" height="164" /&gt; In previous posts, I’ve shown different ways to build a client-side class browser, using the ASP.NET Ajax Libary and jQuery.&lt;/p&gt;  &lt;p&gt;In this post, I’ll focus on a few lines of code from the latest version of that sample. Those few lines of code enable my custom script to benefit from the script loader’s features such as lazy and parallel loading and dependency management.&lt;/p&gt;  &lt;p&gt;An important feature of the script loader is the separation of the script meta-data from the script code itself. The meta-data can include the name of the script, its dependencies, instructions on how to figure out if it’s already loaded, its debug and release URL patterns and a declaration of the lazy components and plug-ins it introduces. The script loader can also handle composite scripts but I won’t cover that in this post.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The &lt;strong&gt;name&lt;/strong&gt; of the script is what will be used to reference it from other scripts and to generate the URL from a pattern. It shouldn’t include the “.js” extension.&lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;dependencies&lt;/strong&gt; and executionDependencies are each a simple array of script names that the script depends on. I’ll explain the difference in a minute.&lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;is loaded&lt;/strong&gt; criterion is a JavaScript expression that returns true if the script is already loaded. This is usually a test on an object that the script file defines. For example, the “templates” script uses !!(Sys.UI &amp;amp;&amp;amp; Sys.UI.Templates). It can assume Sys because it’s defined by start.js. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Debug and release patterns&lt;/strong&gt; are expressions that enable the framework to map script names into debug and release URLs. The ASP.NET Library uses “%/MicrosoftAjax{0}.debug.js” and “%/MicrosoftAjax{0}.js” where % gets replaced with the path where the script loader was downloaded from (this can be CDN or local) and {0} gets replaced with the script name. You can create your own pattern or just provide fixed URLs if you have only one script file. The debug pattern is optional.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Lazy components and plug-ins&lt;/strong&gt; are helpers that the script loader can create for you and that enable developers to start using your components before they are even loaded and hide the script loading aspects as much as possible. For example, it’s that feature that enables you to write this while still having only start.js loaded:       &lt;pre class="code"&gt;Sys.create.dataView(&lt;span style="color: maroon"&gt;&amp;quot;#myDataView&amp;quot;&lt;/span&gt;, {
    data: myData
});&lt;/pre&gt;
The code that needed to be written for this helper to be created was this: 

    &lt;pre class="code"&gt;behaviors: [&lt;span style="color: maroon"&gt;&amp;quot;Sys.UI.DataView&amp;quot;&lt;/span&gt;]&lt;/pre&gt;
This gets automatically transformed by the script loader into the Sys.create.dataView method, before it even tries to load the actual script that defines Sys.UI.DataView. And if you have jQuery loaded as well, you’ll also get a jQuery plug-in out of it for free, which means that you can do: 

    &lt;pre class="code"&gt;$(&lt;span style="color: maroon"&gt;&amp;quot;.data&amp;quot;&lt;/span&gt;).dataView({data: myData});&lt;/pre&gt;
and instantiate DataView controls over the results of a selector query in one operation. Groovy. Of course, you can do the same with the behaviors and controls that you write in your own scripts, with lots of options to customize names, add parameters, etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the case of the class library code, here’s the meta-data declaration code that I had to write:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.loader.defineScript({
  name: &lt;span style="color: maroon"&gt;&amp;quot;classBrowserTree&amp;quot;&lt;/span&gt;,
  releaseUrl: &lt;span style="color: maroon"&gt;&amp;quot;%/TreejQuery.js&amp;quot;&lt;/span&gt;,
  executionDependencies:&lt;br /&gt;    [&lt;span style="color: maroon"&gt;&amp;quot;jQuery&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;Templates&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;ComponentModel&amp;quot;&lt;/span&gt;],
  isLoaded: !!(window.jQuery &amp;amp;&amp;amp;&lt;br /&gt;    window.jQuery.fn.classBrowserTreeView)
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This declares that my script, named “classBrowserTree”, can be found at the URL TreejQuery.js relative to the base URL of the script loader, that it depends on jQuery, Templates and ComponentModel (which themselves have their own dependencies) and that the loader can determine whether it’s already loaded by evaluating the classBrowserTreeView jQuery plug-in.&lt;/p&gt;

&lt;p&gt;Now, loading that script and all its dependencies is as simple as writing this:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.require(Sys.scripts.classBrowserTree);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice that as you’re typing this, you actually get full IntelliSense in Visual Studio on the name of the script, which is great for speed and to avoid typos:&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="ScriptLoaderIntelliSense" border="0" alt="ScriptLoaderIntelliSense" src="http://weblogs.asp.net/blogs/bleroy/ScriptLoaderIntelliSense_10B86D5C.png" width="520" height="387" /&gt; &lt;/p&gt;

&lt;p&gt;One of the things that enable the script loader to do its job is a special way to write your script that makes it possible to separate the loading and parsing of the code from its execution.&lt;/p&gt;

&lt;p&gt;Don’t freak out (yet) though as the code you have to introduce is very lightweight and it doesn’t prevent your script from being loaded &lt;em&gt;without&lt;/em&gt; the script loader (with a plain old script tag for example).&lt;/p&gt;

&lt;p&gt;Reversely, a script that doesn’t have the special script loader code can be handled by the script loader but its dependencies must be declared using “dependencies” instead of “executionDependencies” in the meta-data declaration code, which will in effect disable the more advanced features of the script loader such as parallel loading.&lt;/p&gt;

&lt;p&gt;The special code in question is the following:&lt;/p&gt;

&lt;pre class="code"&gt;(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
  &lt;span style="color: blue"&gt;function &lt;/span&gt;execute() {
    &lt;span style="color: #006400"&gt;// Your code goes here.
&lt;/span&gt;  }
  &lt;span style="color: blue"&gt;if &lt;/span&gt;(window.Sys &amp;amp;&amp;amp; Sys.loader) {
    Sys.loader.registerScript(&lt;br /&gt;      &lt;span style="color: maroon"&gt;&amp;quot;classBrowserTree&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;null&lt;/span&gt;, execute);
  }
  &lt;span style="color: blue"&gt;else &lt;/span&gt;{
    execute();
  }
})();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;What you see here is a (function() {…})(); wrapper, which is standard practice to isolate code from the global namespace (essentially, it’s a local scope); we also have another named scope in there that we arbitrarily call “execute” (but the name doesn’t matter). This is where you’ll typically put your actual code. Then we have the bootstrapping code that looks for the script loader. If it isn’t found, the code in the execute function is immediately run. If it &lt;em&gt;is&lt;/em&gt; found, the execute function is registered with the script loader but is not immediately executed.&lt;/p&gt;

&lt;p&gt;This wrapper code is what enables the script loader to do its magic. Thanks to this little bit of code, it doesn’t care at all in what order the scripts are being loaded, because it can delay the time when any script actually gets executed until all its dependencies have been successfully loaded.&lt;/p&gt;

&lt;p&gt;This also allows the script loader to load scripts in parallel, even if one depends on another. Normally a script loader would have to download a dependent script first and wait for it to completely load before loading the next script. This ‘serializes’ the loading process and is not good for performance. Modern browsers can download 6 to 8 scripts simultaneously. Separating the loading of a script from its execution allows the loader to take advantage of that, and in most cases, even complex dependency trees can be downloaded completely in parallel, meaning the total time is not the sum of each script, but only the longest one.&lt;/p&gt;

&lt;p&gt;Of course, in order to be wrappable, your code needs to be able to run in a non-global scope. This is good practice anyways and is relatively easy to achieve in most cases, so if it can’t, it may be a good idea to try to determine why and fix it.&lt;/p&gt;

&lt;p&gt;And this is it. I hope I’ve convinced you that the script loader can help you to improve the performance of your applications at a really low cost. It also provides a very worthy client-side equivalent of the server-side ScriptManager that ASP.NET has been providing to developers for years: switching the whole application between debug and release scripts is a breeze, which minimizes the risk of deploying debug scripts in production; dependencies are being handled automatically; script combination is handled.&lt;/p&gt;

&lt;p&gt;(many thanks to &lt;a href="http://weblogs.asp.net/infinitiesloop"&gt;Dave Reed&lt;/a&gt; for helping with this post and of course for implementing an awesome script loader)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7262909" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>JavaScript class browser: once again with jQuery</title><link>http://weblogs.asp.net/bleroy/archive/2009/10/30/javascript-class-browser-once-again-with-jquery.aspx</link><pubDate>Fri, 30 Oct 2009 17:43:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7243389</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7243389</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/10/30/javascript-class-browser-once-again-with-jquery.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/MercuryWeb_1CBF87AD.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2004 Bertrand Le Roy" border="0" alt="(c) 2004 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/MercuryWeb_thumb_1373269F.jpg" width="184" height="244" /&gt;&lt;/a&gt; I’ve already posted twice about that little class browser application. The first iteration was mostly declarative and can be found here:    &lt;br /&gt;&lt;a title="http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx" href="http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The second one was entirely imperative and can be found here:   &lt;br /&gt;&lt;a title="http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx" href="http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This new version builds on top of the code for the imperative version and adds the jQuery dependency in an attempt to make the code leaner and simpler. I invite you to refer to the imperative code (included in the &lt;a href="http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQuery.zip"&gt;archive for this post&lt;/a&gt;) and compare it with the jQuery version, which shows a couple of ways the Microsoft Ajax Library lights up when jQuery is present.&lt;/p&gt;  &lt;p&gt;The first thing I want to do here is convert the plain function I was using to build the browser’s namespace and class tree into a jQuery plug-in:&lt;/p&gt;  &lt;pre class="code"&gt;$.fn.classBrowserTreeView = &lt;span style="color: blue"&gt;function &lt;/span&gt;(options) {
  &lt;span style="color: blue"&gt;var &lt;/span&gt;opts = $.extend({},&lt;br /&gt;    $.fn.classBrowserTreeView.defaults,&lt;br /&gt;    options);
  &lt;span style="color: blue"&gt;return this&lt;/span&gt;;
};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That plug-in will have two options: the data to render (which will default to the root namespaces in the Microsoft Ajax Library), and the node template selector (which will default to “#nodeTemplate”):&lt;/p&gt;

&lt;pre class="code"&gt;$.fn.classBrowserTreeView.defaults = {
  data: Type.getRootNamespaces(),
  nodeTemplate: &lt;span style="color: maroon"&gt;&amp;quot;#nodeTemplate&amp;quot;
&lt;/span&gt;};&lt;/pre&gt;

&lt;p&gt;For the moment, as you can see, this plug-in does nothing. We want it to create a DataView control on each of the elements of the current wrapped set. We will do this by calling into the dataView plug-in.&lt;/p&gt;

&lt;p&gt;You may be wondering where this plug-in might come from. Well, that’s the first kind of lighting up that the Microsoft Ajax Library’s script loader (start.js) will do in the presence of jQuery: every control and behavior will get surfaced as a jQuery plug-in, and components will get added as methods on the jQuery object. This is similar to what I had shown a while ago in this post, only much easier:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx" href="http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, we can write this in our own plug-in to create DataView components over the current jQuery wrapped set:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return this&lt;/span&gt;.dataView({
  data: opts.data,
  itemTemplate: opts.nodeTemplate,
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now we can wire up the itemRendered event of the data view and start enriching the markup that the DataView control rendered for each data item. First, let’s get hold of the nodes in the rendered template and wrap them:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;elt = $(args.nodes);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Then, if the current node is representing a namespace, we want to hook up the expansion button’s click event so that it toggles visibility of the list of children, and we want to “unhide” the button itself (it has a “hidden” class in the default markup):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(Type.isNamespace(args.dataItem)) {
  elt.find(&lt;span style="color: maroon"&gt;&amp;quot;.toggleButton&amp;quot;&lt;/span&gt;).click(&lt;span style="color: blue"&gt;function &lt;/span&gt;(e) {
    e.preventDefault();
    &lt;span style="color: blue"&gt;return &lt;/span&gt;toggleVisibility(&lt;span style="color: blue"&gt;this&lt;/span&gt;);
  }).removeClass(&lt;span style="color: maroon"&gt;&amp;quot;hidden&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You can see here that we’re taking advantage of chaining.&lt;/p&gt;

&lt;p&gt;Next thing is to set-up the node link itself. We start by inhibiting the link’s default action. Then we set the text for the link, and finally we set the command that will bubble up to the DataView when the link gets clicked:&lt;/p&gt;

&lt;pre class="code"&gt;elt.find(&lt;span style="color: maroon"&gt;&amp;quot;.treeNode&amp;quot;&lt;/span&gt;).click(&lt;br /&gt;  &lt;span style="color: blue"&gt;function &lt;/span&gt;(e) {&lt;br /&gt;    e.preventDefault();&lt;br /&gt;    &lt;span style="color: blue"&gt;return false&lt;/span&gt;;&lt;br /&gt;  })
  .text(getSimpleName(args.dataItem.getName()))
  .setCommand(&lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here, I’m using a small plug-in to set the command:&lt;/p&gt;

&lt;pre class="code"&gt;$.fn.setCommand = &lt;span style="color: blue"&gt;function &lt;/span&gt;(options) {
  &lt;span style="color: blue"&gt;var &lt;/span&gt;opts = $.extend({},&lt;br /&gt;    $.fn.setCommand.defaults, options);
  &lt;span style="color: blue"&gt;return &lt;/span&gt;$(&lt;span style="color: blue"&gt;this&lt;/span&gt;).each(&lt;span style="color: blue"&gt;function &lt;/span&gt;() {
    $.setCommand(&lt;span style="color: blue"&gt;this&lt;/span&gt;,&lt;br /&gt;      opts.commandName,&lt;br /&gt;      opts.commandArgument,&lt;br /&gt;      opts.commandTarget);
  });
}
$.fn.setCommand.defaults = {
    commandName: &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;,
    commandArgument: &lt;span style="color: blue"&gt;null&lt;/span&gt;,
    commandTarget: &lt;span style="color: blue"&gt;null
&lt;/span&gt;};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I’m using $.setCommand here, which does get created by the framework for me, but I still need to create that small plug-in to make it work on a wrapped set instead of a static method off jQuery. I’ve sent feedback to the team that setCommand and bind should get created as plug-ins by the framework and hopefully it will happen in a future version.&lt;/p&gt;

&lt;p&gt;The last thing we need to do here is to recursively create the child branches of our tree:&lt;/p&gt;

&lt;pre class="code"&gt;elt.find(&lt;span style="color: maroon"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;).classBrowserTreeView({
    data: getChildren(args.dataItem)
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This just finds the child UL element of the current branch and calls our plug-in on the results with the children namespaces and classes as the data.&lt;/p&gt;

&lt;p&gt;And this is it for the tree, we can now create it with this simple call:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: maroon"&gt;&amp;quot;#tree&amp;quot;&lt;/span&gt;).classBrowserTreeView();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The details view rendering will only differ in minor ways from the code we had in our previous imperative version. The only differences is the use of jQuery to traverse and manipulate the DOM instead of the mix of native DOM APIs and Sys.get that we were using before. For example,&lt;/p&gt;

&lt;pre class="code"&gt;args.get(&lt;span style="color: maroon"&gt;&amp;quot;li&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;  args.dataItem.getName ?&lt;br /&gt;    args.dataItem.getName() :&lt;br /&gt;    args.dataItem.name;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;becomes:&lt;/p&gt;

&lt;pre class="code"&gt;$(args.nodes).filter("li").text(&lt;br /&gt;  args.dataItem.getName ?&lt;br /&gt;    args.dataItem.getName() :&lt;br /&gt;    args.dataItem.name);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice how jQuery’s text method makes things a little more secure than the innerHTML we had used before.&lt;/p&gt;

&lt;p&gt;Updating the details view with the data for the item selected in the tree is done by handling the select command of the tree from the following function:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onCommand(sender, args) {
  &lt;span style="color: blue"&gt;if &lt;/span&gt;(args.get_commandName() === &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = sender.findContext(&lt;br /&gt;      args.get_commandSource()).dataItem;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isClass = Type.isClass(dataItem) &amp;amp;&amp;amp;&lt;br /&gt;                 !Type.isNamespace(dataItem);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;childData =&lt;br /&gt;      (isClass ? getMembers : getChildren)(dataItem);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;detailsChild =&lt;br /&gt;      Sys.Application.findComponent(&lt;span style="color: maroon"&gt;&amp;quot;detailsChild&amp;quot;&lt;/span&gt;);
    detailsChild.onItemRendering =&lt;br /&gt;      isClass ?&lt;br /&gt;        onClassMemberRendering :&lt;br /&gt;        onNamespaceChildRendering;
    detailsChild.onItemRendered =&lt;br /&gt;      onDetailsChildRendered;
    detailsChild.set_data(childData);
    $(&lt;span style="color: maroon"&gt;&amp;quot;#detailsTitle&amp;quot;&lt;/span&gt;).text(dataItem.getName());
    $(&lt;span style="color: maroon"&gt;&amp;quot;.namespace&amp;quot;&lt;/span&gt;).css(&lt;br /&gt;      &lt;span style="color: maroon"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;,&lt;br /&gt;      isClass ? &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;);
    $(&lt;span style="color: maroon"&gt;&amp;quot;.class&amp;quot;&lt;/span&gt;).css(&lt;br /&gt;      &lt;span style="color: maroon"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;,&lt;br /&gt;      isClass ? &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;);
    $(&lt;span style="color: maroon"&gt;&amp;quot;#details&amp;quot;&lt;/span&gt;).css(&lt;span style="color: maroon"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;);
  }
}&lt;/pre&gt;

&lt;p&gt;Not much change here from the previous version, again, except for the use of jQuery and some chaining.&lt;/p&gt;

&lt;p&gt;And that is pretty much it. I’ve made other changes in the script to make use of the new script loader in the Microsoft Ajax Library but that will be the subject of a future post.&lt;/p&gt;

&lt;p&gt;Hopefully, this has shown you how the Microsoft Ajax Library can light up with jQuery. The automatic creation of plug-ins feels very much like native jQuery plug-ins and brings all the power of client templates to jQuery. Once we have bind and setCommand plug-ins as well, the Microsoft Ajax Library may become a very useful tool to jQuery programmers just as much as jQuery itself is a very useful tool to Microsoft Ajax programmers.&lt;/p&gt;

&lt;p&gt;The code can be found here:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQueryUpdated.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQueryUpdated.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQueryUpdated.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; fixed a problem in Firefox &amp; Chrome.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7243389" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>How to render the same template on the server and client with minimal redundancy</title><link>http://weblogs.asp.net/bleroy/archive/2009/10/19/how-to-render-the-same-template-on-the-server-and-client-with-minimal-redundancy.aspx</link><pubDate>Mon, 19 Oct 2009 08:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7232857</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7232857</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/10/19/how-to-render-the-same-template-on-the-server-and-client-with-minimal-redundancy.aspx#comments</comments><description>&lt;h4&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="right" src="http://weblogs.asp.net/blogs/bleroy/buildingreflection_6007098A.jpg" width="180" height="260" /&gt; &lt;/h4&gt;  &lt;p&gt;Last week, I wrote &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx"&gt;a post about how the new Microsoft Ajax Library Preview 6 made it a lot easier to write unobtrusive and imperative data-driven applications&lt;/a&gt;. Because for the previous preview, I had written a cool little class browser using a declarative style, I thought it would be nice to rewrite this in a completely imperative way. The mistake I made though was to call it unobtrusive. Never mind that ‘unobtrusive’ is a perfectly well-defined word that actually existed way before JavaScript. ‘Unobtrusive JavaScript’ has a very specific meaning that people feel strongly about. To be worthy of that label, an application must basically conform to (at least) those two requirements:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Markup and behavior are strictly separated.&lt;/strong&gt; That means no DOM-0 event handlers, no custom attributes or tags, and even no microformats imo. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Graceful degradation / progressive enhancement.&lt;/strong&gt; This means that the application’s script is only used to improve what would work without script, in other words that the application is entirely usable without script. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;While my little sample strictly conformed to (1), it didn’t conform to (2). Not because I’m too dumb or ignorant, mind you, but because for this application, it didn’t make any sense at all: the application’s whole point is to display client-side script objects. The server does not have the first clue about the data that needs to be rendered. Which makes it impossible for anything to render without script. Which in turn triggered some unpleasant comments on the post: this was not really unobtrusive JavaScript in the full sense of the term. Not the library’s fault though, just my own for using the wrong example.&lt;/p&gt;  &lt;h4&gt;The right example&lt;/h4&gt;  &lt;p&gt;So I thought the best thing to fix this is to provide a more relevant example, one where the server could actually be a fallback scenario.&lt;/p&gt;  &lt;p&gt;The new sample code is a fairly simple master-details view on a silly data set: jedi data. We have a WCF web service that is returning a list of jedis and their associated data, which the application can render on the server-side or on the client-side.&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="Jedis" border="0" alt="Jedis" src="http://weblogs.asp.net/blogs/bleroy/Jedis_0779A810.png" width="413" height="176" /&gt; Once you&amp;#160; have data that is available from both the server and client sides, your best and simplest tool to achieve progressive enhancement is the plain &amp;lt;a&amp;gt; tag. You can build the links in your application so that without script, they do something meaningful:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;?jedi=all&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;expandButton&amp;quot;&amp;gt;&lt;/span&gt;+&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;What you see above is the + link on the top-left of the screen that will expand the list of jedis. To make it work client-side instead of the server-side, you use script to add a click handler that suppresses the default behavior of the link and replaces it with the equivalent client-side action:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.UI.DomEvent.addHandler(&lt;br /&gt;  Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#expandButton&amp;quot;&lt;/span&gt;), &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;function&lt;/span&gt;(e) {
    e.preventDefault();
    jediList.fetchData();
    &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
  });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That’s fairly easy and has been possible since, well, I’m not sure but it’s been a looong time.&lt;/p&gt;

&lt;p&gt;Now there’s the templated rendering. Rendering the same thing from the server and the client without repeating oneself too much is not as simple. The key to it is to render the server template with an empty data item and then to use the result of that as the client template.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediList&amp;quot;&lt;br /&gt;&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;if (!isDetails) { &lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt; &lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;jedis = !isDetails ?
    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Jedi&lt;/span&gt;&amp;gt;() {&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Jedi&lt;/span&gt;()} :
    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;JediService&lt;/span&gt;().GetJedis();
  &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;jedi &lt;span style="color: blue"&gt;in &lt;/span&gt;jedis) { &lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;?&lt;span style="color: red"&gt;jedi&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;= &lt;/span&gt;jedi.Name &lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;quot;&amp;gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;= &lt;/span&gt;jedi.Name&lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;} &lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This way, there is only one template, only one version of the markup, that we are using on both the server and client sides. When rendered from the server with the actual data set, we get the list right away, and the browser just displays it (and search engines can see the list as well, something you can’t achieve with pure script). When rendered with the dummy dataset, we get the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediList&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;  &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;?jedi&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: blue"&gt;all&amp;quot;&amp;gt;all&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This markup can be used as a template on the client-side by this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;jediList = Sys.create.dataView(&lt;span style="color: maroon"&gt;&amp;quot;#jediList&amp;quot;&lt;/span&gt;, {
  dataProvider: &lt;span style="color: maroon"&gt;&amp;quot;JediService.svc&amp;quot;&lt;/span&gt;,
  fetchOperation: &lt;span style="color: maroon"&gt;&amp;quot;GetJedis&amp;quot;&lt;/span&gt;,
  autoFetch: &lt;span style="color: blue"&gt;false&lt;/span&gt;,
  itemRendered: &lt;span style="color: blue"&gt;function&lt;/span&gt;(sender, args) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;link = args.get(&lt;span style="color: maroon"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = args.dataItem;
    link.innerHTML = dataItem.Name;
    Sys.UI.DomEvent.addHandler(&lt;br /&gt;      link, &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;function&lt;/span&gt;(e) {
        e.preventDefault();
        details.set_data(dataItem);
        &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
      });
  }
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The details view is built on pretty much the same principle. The main difference is that it does have additional markup to delimit the fields that we’ll want to set dynamically. Here’s how it renders with the dummy data:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;details&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediName&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;owns a
    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediLightSaber&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;lightsaber and is on the
    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediSide&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;side.
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;All the itemRendered handler has to do then is fill in the blanks:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;details = Sys.create.dataView(&lt;span style="color: maroon"&gt;&amp;quot;#details&amp;quot;&lt;/span&gt;, {
  itemRendered: &lt;span style="color: blue"&gt;function&lt;/span&gt;(sender, args) {
    args.get(&lt;span style="color: maroon"&gt;&amp;quot;#jediName&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      args.dataItem.Name;
    args.get(&lt;span style="color: maroon"&gt;&amp;quot;#jediLightSaber&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      args.dataItem.LightsaberColor;
    args.get(&lt;span style="color: maroon"&gt;&amp;quot;#jediSide&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      args.dataItem.DarkSide ? &lt;span style="color: maroon"&gt;&amp;quot;dark&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;light&amp;quot;&lt;/span&gt;;
  }
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;So what do you think?&lt;/p&gt;

&lt;p&gt;Here’s the code (by the way, I’m using the Microsoft CDN in there): 
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/ServerClientTemplates.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/ServerClientTemplates.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/ServerClientTemplate.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7232857" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Entirely unobtrusive and imperative templates with Microsoft Ajax Library Preview 6</title><link>http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx</link><pubDate>Thu, 15 Oct 2009 09:12:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7230434</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7230434</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/IMG_2305_0DB1EDD9.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) 2009 Bertrand Le Roy" border="0" alt="(c) 2009 Bertrand Le Roy" align="right" src="http://weblogs.asp.net/blogs/bleroy/IMG_2305_thumb_138C9172.jpg" width="244" height="164" /&gt;&lt;/a&gt; Today is the release of the sixth preview of Microsoft Ajax Library. Don’t get fooled by the somewhat silly and long name: this is a major release in many ways. The scripts have been majorly refactored since preview 5. Check out the other posts out there (links at the bottom of this post) to see just some of the many new features that are in there. Some of my favorite are all the small improvements that have been made to make imperative instantiation of components and templated contents easier than ever. Many of you have told us that you preferred to do things imperatively and this release makes it a lot better.&lt;/p&gt;  &lt;p&gt;When Preview 5 came out, I built a simple class browser using the declarative syntax. The class browser shows the hierarchy of namespaces and classes in a tree view on the left side of the page, and the details of whatever’s selected in the tree on the right side of the page:&lt;a href="http://weblogs.asp.net/blogs/bleroy/ClassBrowser2_36D98617.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px auto; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The JavaScript class browser" border="0" alt="The JavaScript class browser" src="http://weblogs.asp.net/blogs/bleroy/ClassBrowser2_thumb_23B8596B.png" width="504" height="187" /&gt;&lt;/a&gt;&lt;a href="http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It still works (and an updated version is attached to this post), but I thought I would demonstrate how you can take that same sample and re-implement it in a completely imperative way. Of course, you never have to go all the way one way or another, and it’s always possible for example to use the nice declarative syntax for bindings but instantiate your components imperatively if you choose to do so. In this post, I’m deliberately going imperative all the way. Just keep in mind this is rather extreme.&lt;/p&gt;  &lt;p&gt;The first thing to notice in the new version is that the markup is perfectly clean and contains no weird extension, namespace or custom binding syntax whatsoever. It’s 100% pure HTML 5. Here is for example the complete markup for the tree view on that page:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;tree&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;tree&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;nodeTemplate&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;toggleButton&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#&amp;quot;&amp;gt;&lt;/span&gt;+&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;treeNode&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The script that builds the dynamic contents is bootstrapped by the following code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Scripts/start.js&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
&lt;/span&gt;Sys.loadScripts([&amp;quot;Scr&lt;span style="color: maroon"&gt;ipts/Tree.js&amp;quot;], f&lt;/span&gt;unc&lt;span style="color: blue"&gt;tion() {
  &lt;/span&gt;Sys.require([Sys.components.dataView], function&lt;span style="color: blue"&gt;() {
    &lt;/span&gt;createTreeView(&amp;quot;#tree&amp;quot;,&lt;br /&gt;                   Typ&lt;span style="color: maroon"&gt;e.getRo&lt;/span&gt;otNamespaces(),&lt;br /&gt;                   &amp;quot;#nodeTempla&lt;span style="color: maroon"&gt;te&amp;quot;);
    &lt;/span&gt;Sys.create.dataView(&amp;quot;#detailsChild&amp;quot;, &lt;span style="color: maroon"&gt;{
      &lt;/span&gt;itemRendered: onDetailsChildRendered
    });
  });
});
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We’re making use of the new script loader here: we first include the bootstrapper file, start.js, and then we declare that we need one custom script, “tree.js” and everything necessary to instantiate a DataView. The script loader will figure out on its own the set of scripts it needs to download for that. Once those scripts have been downloaded, we call createTreeView, which is custom code that we’ll look at in a moment that creates nested DataView controls over the markup. We also create a second DataView to display the details of what’s selected in the&amp;#160; tree.&lt;/p&gt;

&lt;p&gt;Notice that we set some properties to a selector string here (for example “#nodeTemplate”). This is actually a breaking change from the previous preview, which only understood id strings. Microsoft Ajax does not include a full selector engine but it does understand the most basic of selectors (.class, tagName and #id). But where it gets really interesting is that if you had included jQuery on the page, the framework would detect it and enable you to use full selectors everywhere. Isn’t that sweet?&lt;/p&gt;

&lt;p&gt;So how does the imperative approach compare with the declarative one? Well, for instantiating components, you already have an example above, where we use Sys.create.dataView. But what about wiring up events, setting text contents and attribute values, instantiating components over the markup inside the template?&lt;/p&gt;

&lt;p&gt;All those are done by post-processing the template instances after they’ve been instantiated, by handling the itemRendered event:&lt;/p&gt;

&lt;pre class="code"&gt;itemRendered: &lt;span style="color: blue"&gt;function&lt;/span&gt;(sender, args) {
  &lt;span style="color: #006400"&gt;// do magic
&lt;/span&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Wiring up events is as simple as getting a reference to an element and calling addHandler:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;toggleButton = args.get(&lt;span style="color: maroon"&gt;&amp;quot;.toggleButton&amp;quot;&lt;/span&gt;);
Sys.UI.DomEvent.addHandler(toggleButton, &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: blue"&gt;function&lt;/span&gt;(e) {
    toggleVisibility(&lt;span style="color: blue"&gt;this&lt;/span&gt;);
  }, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The args.get function, which you will use a lot, takes a selector and returns the first element that matches it &lt;em&gt;inside the template&lt;/em&gt;. Here, we are looking for an element with class “toggleButton”, but a local id would work just as well.&lt;/p&gt;

&lt;p&gt;To set text contents and attribute values is trivial once you know how to get references to elements from local selectors (remember, jQuery also works here transparently or even explicitly when and if you need it).&lt;/p&gt;

&lt;p&gt;Finally, instantiating components is also quite easy. For example, here is the code that creates an inner DataView for the child nodes of a node in the tree, recursively:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;childView = args.get(&lt;span style="color: maroon"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;);
createTreeView(childView,&lt;br /&gt;               getChildren(args.dataItem),&lt;br /&gt;               nodeTemplate);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The args.get funtion is used once more to get a reference to the first UL element within the template, and it is then easy to do a recursive call into our tree creation function and build the new branch of the tree.&lt;/p&gt;

&lt;p&gt;The command bubbling feature that makes it so easy to wire up custom commands into a template is still usable in imperative code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;treeNode = args.get(&lt;span style="color: maroon"&gt;&amp;quot;.treeNode&amp;quot;&lt;/span&gt;);
Sys.setCommand(treeNode, &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Finally, there is one feature that I’m not using in that sample, but that’s immensely useful, and I’m talking of course of live bindings. Those work too, all you have to do is call the Sys.bind function and give it the target object, the name of the target property to bind, the source object and the source property name.&lt;/p&gt;

&lt;p&gt;To render the details view, I decided to not use a single item DataView like I did with the declarative version: since I’m going to use imperative code instead of declarative bindings, it is just as easy to directly manipulate the DOM that already exists, and do some hiding and showing of elements:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onCommand(sender, args) {
  &lt;span style="color: blue"&gt;if &lt;/span&gt;(args.get_commandName() === &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = sender.findContext(&lt;br /&gt;      args.get_commandSource()).dataItem;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isClass = Type.isClass(dataItem) &amp;amp;&amp;amp;&lt;br /&gt;                 !Type.isNamespace(dataItem);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;childData =&lt;br /&gt;      (isClass ? getMembers : getChildren)(dataItem),
      namespaceElementsDisplay = &lt;br /&gt;        isClass ? &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;,
      classElementsDisplay =&lt;br /&gt;        isClass ? &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;,
      detailsChild =&lt;br /&gt;        Sys.Application.findComponent(&lt;span style="color: maroon"&gt;&amp;quot;detailsChild&amp;quot;&lt;/span&gt;);
    detailsChild.onItemRendering =&lt;br /&gt;      isClass ?&lt;br /&gt;        onClassMemberRendering :&lt;br /&gt;        onNamespaceChildRendering;
    detailsChild.set_data(childData);
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#detailsTitle&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      dataItem.getName();
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#namespacesColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#classesColumn&amp;quot;&lt;/span&gt;).style.display =&lt;br /&gt;      namespaceElementsDisplay;
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#propertiesColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#eventsColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#methodsColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#staticMethodsColumn&amp;quot;&lt;/span&gt;).style.display =&lt;br /&gt;      classElementsDisplay;
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#details&amp;quot;&lt;/span&gt;).style.display = &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;;
  }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We do have a DataView to render the contents of the currently selected object though. The nice trick we used with the declarative version to dynamically switch the target place holder where the template gets rendered is still there, which enables a single DataView control to dispatch the data into two to four separate lists (or however much you want for that matter):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onNamespaceChildRendering(args) {
    args.set_itemPlaceholder(
        Type.isClass(args.get_dataItem()) ?
            &lt;span style="color: maroon"&gt;&amp;quot;#classPlaceHolder&amp;quot; &lt;/span&gt;:
            &lt;span style="color: maroon"&gt;&amp;quot;#namespacePlaceHolder&amp;quot;
    &lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I think all this is pretty cool and I hope the comparison between the declarative version and the imperative version of this little application gives you a sense of the flexibility that the Microsoft Ajax library now offers, and of how much you can choose your own development style and do pretty much anything with the same ease.&lt;/p&gt;

&lt;p&gt;Download the code for this post here: 
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Preview6ClassBrowser.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Preview6ClassBrowser.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Preview6ClassBrowser.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Microsoft Ajax Library Preview 6 can be downloaded from here:
  &lt;br /&gt;&lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34488"&gt;http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34488&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are a few links about this release:
 &lt;br/&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2009/10/15/announcing-microsoft-ajax-library-preview-6-and-the-microsoft-ajax-minifier.aspx"&gt;http://weblogs.asp.net/scottgu/archive/2009/10/15/announcing-microsoft-ajax-library-preview-6-and-the-microsoft-ajax-minifier.aspx&lt;/a&gt;
  &lt;br /&gt;&lt;a href="http://channel9.msdn.com/posts/jsenior/Announcing-Microsoft-Ajax-Library-Preview-6/"&gt;http://channel9.msdn.com/posts/jsenior/Announcing-Microsoft-Ajax-Library-Preview-6/&lt;/a&gt;

  &lt;br /&gt;&lt;a href="http://www.jamessenior.com/post/How-the-Script-Loader-in-the-Microsoft-Ajax-Library-will-make-your-life-wonderful.aspx"&gt;http://www.jamessenior.com/post/How-the-Script-Loader-in-the-Microsoft-Ajax-Library-will-make-your-life-wonderful.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7230434" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Fun with C# 4.0’s dynamic</title><link>http://weblogs.asp.net/bleroy/archive/2009/09/17/fun-with-c-4-0-s-dynamic.aspx</link><pubDate>Thu, 17 Sep 2009 07:50:29 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7209018</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7209018</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/09/17/fun-with-c-4-0-s-dynamic.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="right" src="http://weblogs.asp.net/blogs/bleroy/DSCN2236Touched_14096A29.jpg" width="244" height="184" /&gt;There’s been &lt;a href="http://haacked.com/archive/2009/08/26/method-missing-csharp-4.aspx"&gt;some&lt;/a&gt; &lt;a href="http://haacked.com/archive/2009/08/31/7-stages-of-language-keyword-grief.aspx"&gt;debate&lt;/a&gt; &lt;a href="http://blog.wekeroad.com/blog/magical-dynamic-mystery-tour/"&gt;recently&lt;/a&gt; on &lt;a href="http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx"&gt;the new “dynamic” keyword in C# 4.0&lt;/a&gt;. As has been the case with many features before it, some love it, some hate it, some say it bloats the language, yadda yadda yadda. People said that about lambdas. Me, I’ll just use it where I see a use case, thank you very much.&lt;/p&gt;  &lt;p&gt;In the case of dynamic, another frequent comment is that a statically-typed language should not try to look like a dynamic language. Well, I just don’t believe in that distinction.&lt;/p&gt;  &lt;p&gt;Being dynamic is a &lt;em&gt;trait&lt;/em&gt; that a language can have, and some have it more than others. But as soon as a language has a dictionary type or indexers, and most modern languages do, it starts having dynamicity. What people call a dynamic language is just one where it’s the only available type.&lt;/p&gt;  &lt;p&gt;Now there is type safety of course, but to me that’s a different feature that is not as coupled with “statically-typed-ness” as we usually tend to think. Case in point, many modern JavaScript engines analyze the code in order to apply many of the optimization techniques that static languages have been applying at compile-time.&lt;/p&gt;  &lt;p&gt;So just how dynamic was C# before 4.0? Well, there are dictionaries and indexers, as I’ve said, but there are also other interesting and less known features such as &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.customtypedescriptor.aspx"&gt;custom type descriptors&lt;/a&gt;. Go &lt;a href="http://msdn.microsoft.com/en-us/library/ms171819.aspx"&gt;look them up&lt;/a&gt; if you don’t know about them. Type descriptors are for example used to provide a level of indirection between a design surface and the components being edited.&lt;/p&gt;  &lt;p&gt;But type descriptors can also be built completely dynamically and decide to expose a completely virtual object model that is entirely built at runtime. Of course, the client code for that object has to go through the right APIs to make use of that quasi-dynamic model. And of course those APIs are not simple. Implementing a custom type descriptor in itself is not exactly trivial.&lt;/p&gt;  &lt;p&gt;If anything, the dynamic keyword is going to provide a friendlier syntax for this sort of thing. One problem is that it has no relationship whatsoever with custom type descriptors.&lt;/p&gt;  &lt;p&gt;So I thought I’d try to bridge that gap. How can we take a type that exposes a custom type descriptor, such as &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.datarowview.aspx"&gt;DataRowView&lt;/a&gt;, and transform that into something that plays nice with the dynamic keyword? What we want to be able to write is something like the following:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;table = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataTable&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;People&amp;quot;&lt;/span&gt;);
table.Columns.Add(&lt;span style="color: #a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;));
table.Columns.Add(&lt;span style="color: #a31515"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;));
table.Columns.Add(&lt;span style="color: #a31515"&gt;&amp;quot;Children&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;));
table.LoadDataRow(&lt;br /&gt;    &lt;span style="color: blue"&gt;new object&lt;/span&gt;[] {&lt;span style="color: #a31515"&gt;&amp;quot;Bertrand&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Le Roy&amp;quot;&lt;/span&gt;, 2},&lt;br /&gt;    &lt;span style="color: #2b91af"&gt;LoadOption&lt;/span&gt;.OverwriteChanges);
&lt;span style="color: blue"&gt;var &lt;/span&gt;tableView = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataView&lt;/span&gt;(table);&lt;br /&gt;
&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;dynRecord = &lt;br /&gt;    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TypeDescriptorDynamicWrapper&lt;/span&gt;(tableView[0]);&lt;span style="color: #2b91af"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;{0} {1} has {2} children.&amp;quot;&lt;/span&gt;, 
    dynRecord.FirstName, 
    dynRecord.LastName, 
    dynRecord.Children);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To do that, I built the TypeDescriptorDynamicWrapper that you see being used above. What is does is pretty simple, as it just inherits from DynamicObject and implements TryGetMember as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryGetMember(&lt;br /&gt;    &lt;span style="color: #2b91af"&gt;GetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result) {&lt;br /&gt;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;name = binder.Name;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;property = _properties.Find(
        p =&amp;gt; p.Name.Equals(name,
            binder.IgnoreCase ?
            &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase :
            &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.Ordinal));
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(property == &lt;span style="color: blue"&gt;null&lt;/span&gt;) {
        result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
    }
    result = property.GetValue(&lt;br /&gt;        _descriptor.GetPropertyOwner(&lt;span style="color: blue"&gt;null&lt;/span&gt;));
    &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, the method just finds the right property on the type descriptor and delegates to it if it finds it. I didn’t implement the setting of properties but it would be fairly easy to do so.&lt;/p&gt;

&lt;p&gt;Using that simple wrapper, the program above just works and displays:&lt;/p&gt;

&lt;p style="background-color: black; font-family: courier; color: white"&gt;Bertrand Le Roy has 2 children.&lt;/p&gt;

&lt;p&gt;That was easy. Now what about the reverse, getting any dynamic object to expose a custom type descriptor?&lt;/p&gt;

&lt;p&gt;Well, that’s where things get tricky.&lt;/p&gt;

&lt;p&gt;The dynamic capabilities of C# 4.0 are targeted at two things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Providing syntactic sugar for getting to dynamic members that looks like accessing statically-defined ones.&lt;/li&gt;

  &lt;li&gt;An easy way to create dynamic types by implementing a form of method_missing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But there is one thing that exists in &lt;a href="http://dlr.codeplex.com/"&gt;DLR&lt;/a&gt; but didn’t make it to C# yet, which is the ability to easily reflect on dynamic objects (other than by using the #1 syntactic sugar above).&lt;/p&gt;

&lt;p&gt;For example, in JavaScript, a.foo and a[“foo”] are exactly the same thing, which enables easy reflection: you can enumerate the members of an arbitrary object and do stuff like a[someStringVariable].&lt;/p&gt;

&lt;p&gt;Doing the same thing with C# on dynamic objects is much more difficult. The equivalent of a.foo is easy, but not the equivalent of a[someStringVariable].&lt;/p&gt;

&lt;p&gt;Forget about using reflection, it doesn’t work on dynamic objects because &lt;em&gt;dynamic really is a compiler trick&lt;/em&gt;. There is no such thing as a “dynamic” type to reflect on. It’s a keyword that tells the compiler to relax type-checking and generate code to access those dynamic members at runtime. If you try to reflect on an instance of one of the dynamic-friendly type, such as ExpandoObject, you’ll see the statically-defined members of that type, but not the dynamic ones. In a way, you’re looking at the messenger instead of the message.&lt;/p&gt;

&lt;p&gt;This is a problem because in order to wrap an arbitrary dynamic object into a custom type descriptor, we need to be able to reflect on the dynamic members of that object.&lt;/p&gt;

&lt;p&gt;To solve the problem, and with some help from the nice folks who are building the DLR, I compiled a simple program that sets and gets a property on a dynamic object, an then I looked at the generated code from Reflector (“luckily”, Reflector is not yet aware of dynamic and shows the generated code instead of something closer to the actual code you wrote).&lt;/p&gt;

&lt;p&gt;I was then able to refactor that code and replace the string constants for the accessed member with a method parameter, thus creating a generic way to access dynamic object properties:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static object &lt;/span&gt;GetValue(&lt;br /&gt;    &lt;span style="color: blue"&gt;object &lt;/span&gt;dyn, &lt;span style="color: blue"&gt;string &lt;/span&gt;propName) {&lt;br /&gt;
    &lt;span style="color: green"&gt;// Warning: this is rather expensive,&lt;br /&gt;    // and should be cached in a real app
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;GetterSite = &lt;br /&gt;        &lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&lt;br /&gt;        .Create(
          &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpGetMemberBinder&lt;/span&gt;(propName,
            dyn.GetType(),
            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;[] {
              &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;(&lt;br /&gt;                  &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.None, &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            }));
    &lt;span style="color: blue"&gt;return &lt;/span&gt;GetterSite.Target(GetterSite, dyn);
}

&lt;span style="color: blue"&gt;public static void &lt;/span&gt;SetValue(&lt;br /&gt;    &lt;span style="color: blue"&gt;object &lt;/span&gt;dyn, &lt;span style="color: blue"&gt;string &lt;/span&gt;propName, &lt;span style="color: blue"&gt;object &lt;/span&gt;val) {&lt;br /&gt;
    &lt;span style="color: green"&gt;// Warning: this is rather expensive,&lt;br /&gt;    // and should be cached in a real app
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;SetterSite = &lt;br /&gt;      &lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&lt;br /&gt;      .Create(
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpSetMemberBinder&lt;/span&gt;(propName,
          dyn.GetType(),
          &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;[] {
            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;(&lt;br /&gt;              &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.None, &lt;span style="color: blue"&gt;null&lt;/span&gt;),
            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;(&lt;br /&gt;              &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.LiteralConstant |
              &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.UseCompileTimeType,&lt;br /&gt;              &lt;span style="color: blue"&gt;null&lt;/span&gt;)
          }));
    SetterSite.Target(SetterSite, dyn, val);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Once we have that, implementing the custom type provider is relatively straightforward. The only part that is not immediately obvious is how to enumerate the properties of the dynamic object.&lt;/p&gt;

&lt;p&gt;This is done as follows. Dynamic objects implement &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider(VS.100).aspx"&gt;IDynamicMetaObjetProvider&lt;/a&gt;, which has a &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider.getmetaobject(VS.100).aspx"&gt;GetMetaObject&lt;/a&gt; method, which returns a &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject(VS.100).aspx"&gt;DynamicMetaObject&lt;/a&gt; object on which you can call &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject.getdynamicmembernames(VS.100).aspx"&gt;GetDynamicMemberNames&lt;/a&gt; to get the list of members.&lt;/p&gt;

&lt;p&gt;We can now take any dynamic object and for example present it in a property grid, which is one of those components that know how to handle custom type descriptors but not the new dynamic objects:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;person = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ExpandoObject&lt;/span&gt;();
person.FirstName = &lt;span style="color: #a31515"&gt;&amp;quot;Bertrand&amp;quot;&lt;/span&gt;;
person.LastName = &lt;span style="color: #a31515"&gt;&amp;quot;Le Roy&amp;quot;&lt;/span&gt;;
person.Children = 2;
propertyGrid1.SelectedObject = &lt;br /&gt;    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicTypeDescriptorWrapper&lt;/span&gt;(person);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And here’s what the results look like:&lt;img style="border-bottom: 0px; border-left: 0px; margin: 10px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="PropertyGrid displaying a dynamic object." border="0" alt="PropertyGrid displaying a dynamic object." src="http://weblogs.asp.net/blogs/bleroy/DynamicPropertyGrid_5E24750E.png" width="304" height="207" /&gt; &lt;/p&gt;

&lt;p&gt;In conclusion, I’d say that although we are having a healthy debate on the dynamic keyword (and we should have that debate), I’m confident that in one or two years, we’ll have some incredibly imaginative uses of the feature that will simply blow us away and will make the current conversation a little sillier than it seems today. The feature itself is also in its infancy and not only will the use cases emerge way beyond what even its designers envisioned, it will also grow and become more usable with future versions of the language, as is made clear by the gap in functionality that still exists today with the full DLR project.&lt;/p&gt;

&lt;p&gt;You can download the code for this article here:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Bleroy.Sample.Dynamic.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Bleroy.Sample.Dynamic.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Bleroy.Sample.Dynamic.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7209018" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Dynamic+languages/default.aspx">Dynamic languages</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category></item><item><title>querySelectorAll on old IE versions: something that doesn’t work</title><link>http://weblogs.asp.net/bleroy/archive/2009/08/31/queryselectorall-on-old-ie-versions-something-that-doesn-t-work.aspx</link><pubDate>Tue, 01 Sep 2009 01:03:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7187174</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7187174</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/08/31/queryselectorall-on-old-ie-versions-something-that-doesn-t-work.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 10px 10px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2005" border="0" alt="(c) Bertrand Le Roy 2005" align="right" src="http://weblogs.asp.net/blogs/bleroy/WallDoor_7CF5A223.jpg" width="224" height="244" /&gt; In today’s post, I’m going to show an interesting technique to solve a problem and then I will tear it to pieces and explain why it is actually useless. I believe that negative results should also be published so that we can save other people from wasting time trying the same thing. So here goes…&lt;/p&gt;  &lt;p&gt;A few days ago, &lt;a href="http://ajaxian.com/archives/creating-a-queryselector-for-ie-that-runs-at-native-speed"&gt;a post on Ajaxian&lt;/a&gt; proposed a new version of a somewhat old technique to implement &lt;a href="http://www.w3.org/TR/2007/WD-selectors-api-20071221/"&gt;querySelectorAll&lt;/a&gt; on old versions of IE, using the browser’s native CSS engine. That sounds like a great idea at first, and the hack is quite clever. The idea is to dynamically add a CSS rule to the document that has the selector that you want to evaluate, and an expression that adds the matched elements to a global array.&lt;/p&gt;  &lt;p&gt;When I read this, it reminded me of a similar approach that I had tried a few years ago. At the time, we were considering implementing our own selector engine (we had not yet decided to integrate jQuery to our Ajax offerings, which in the end made the whole effort moot) so we explored a number of approaches.&lt;/p&gt;  &lt;p&gt;My idea was different in that it doesn’t use expressions at all. It does dynamically create a style rule, but instead of an expression, it just sets a non-existing “foo” style property to the equally arbitrary value of “bar”. It then scans the whole document (using the much decried and IE-specific but very fast document.all) and gets the computed style for each of the elements. We then look for the foo property on the resulting object and check whether it evaluates as “bar”. For each element that matches, we add to an array.&lt;/p&gt;  &lt;p&gt;Here’s the code:&lt;/p&gt;  &lt;pre class="code"&gt;(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;style = document.styleSheets[0] ||&lt;br /&gt;                document.createStyleSheet();
    window.select = &lt;span style="color: blue"&gt;function&lt;/span&gt;(selector) {
        style.addRule(selector, &lt;span style="color: #a31515"&gt;&amp;quot;foo:bar&amp;quot;&lt;/span&gt;);
        &lt;span style="color: blue"&gt;var &lt;/span&gt;all = document.all, resultSet = [];
        &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;i = 0, l = all.length; i &amp;lt; l; i++) {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(all[i].currentStyle.foo === &lt;span style="color: #a31515"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;) {
                resultSet[resultSet.length] = all[i];
            }
        }
        style.removeRule(0);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;resultSet;
    }
})();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;or, in minimized form:&lt;/p&gt;

&lt;pre class="code"&gt;(&lt;span style="color: blue"&gt;function&lt;/span&gt;(){&lt;span style="color: blue"&gt;var &lt;/span&gt;d=document;&lt;span style="color: blue"&gt;var &lt;/span&gt;a=d.styleSheets[0]||&lt;br /&gt;d.createStyleSheet();window.select=&lt;span style="color: blue"&gt;function&lt;/span&gt;(e){&lt;br /&gt;a.addRule(e,&lt;span style="color: #a31515"&gt;&amp;quot;f:b&amp;quot;&lt;/span&gt;);&lt;span style="color: blue"&gt;var &lt;/span&gt;l=d.all,c=[];&lt;br /&gt;&lt;span style="color: blue"&gt;for&lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;b=0,f=l.length;b&amp;lt;f;b++)&lt;span style="color: blue"&gt;if&lt;/span&gt;(l[b].currentStyle.f)&lt;br /&gt;c[c.length]=l[b];a.removeRule(0);&lt;span style="color: blue"&gt;return &lt;/span&gt;c}})()&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That’s 235 characters, which is not too bad (although not quite #twitcode small).&lt;/p&gt;

&lt;p&gt;The first problem with that approach though is that because it’s using the native CSS selection engine in IE, it has the same limitations and quirks. That means no fancy CSS 3 (or even 2) selectors. It also means any IE bug will surface into the result set.&lt;/p&gt;

&lt;p&gt;In other words, if you want more selectors than that, you will need to parse the selector string and branch off the code to another, more complete engine whenever something not supported is used. It also means that you need to know what is supported and what isn’t. That could be done through some dynamic discovery but doing so, we are getting into much complexity.&lt;/p&gt;

&lt;p&gt;So limited as it is, how does it perform?&lt;/p&gt;

&lt;p&gt;I ran the code in a &lt;a href="http://code.google.com/p/slickspeed/"&gt;SlickSpeed&lt;/a&gt; test (where I removed the selectors that it couldn’t handle) on IE6 and the good news is that despite the document.all scan and the current style computation, it’s more than three times faster than &lt;a href="http://ajaxian.com/archives/creating-a-queryselector-for-ie-that-runs-at-native-speed"&gt;Paul Young’s implementation that got featured on Ajaxian&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But the bad news is that it’s also &lt;strong&gt;six times slower than jQuery:&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="SlickSpeedResults" border="0" alt="SlickSpeedResults" src="http://weblogs.asp.net/blogs/bleroy/SlickSpeedResults_62B538FF.png" width="540" height="484" /&gt; &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’m afraid a hack to use the native CSS selection engine of the browser is always going to be slower than an optimized pure JavaScript implementation (to be clear, I’m not talking about native implementations of querySelectorAll, but about hacks such as this which try to surface the feature on older IE versions that don’t have querySelectorAll). Somewhat counter-intuitive, but true.&lt;/p&gt;

&lt;p&gt;End of story. Just use jQuery. :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7187174" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CSS/default.aspx">CSS</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/TwitCode/default.aspx">TwitCode</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category></item><item><title>Mandelbrot set in a twitcode</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/10/mandelbrot-set-in-a-twitcode.aspx</link><pubDate>Sat, 11 Jul 2009 00:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7145174</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7145174</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/10/mandelbrot-set-in-a-twitcode.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://twitter.com/KiniK"&gt;Kinik&lt;/a&gt; just published &lt;a href="http://twitter.com/KiniK/statuses/2575582146"&gt;a pretty amazing #twitcode version&lt;/a&gt; of a &lt;a href="http://en.wikipedia.org/wiki/Mandelbrot_set"&gt;Mandelbrot set&lt;/a&gt; visualization in JavaScript that &lt;a href="http://blog.nihilogic.dk/2008/09/mandelbrot-in-less-than-128-bytes-of.html"&gt;Jacob Seidelin&lt;/a&gt; wrote.&lt;/p&gt;  &lt;p&gt;Here’s the code:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;for&lt;/span&gt;(k=84;k-=1/32;document.write(k%3?i%8:&lt;span style="color: #a31515"&gt;'&amp;lt;br/&amp;gt;'&lt;/span&gt;))&lt;br /&gt;&lt;span style="color: blue"&gt;for&lt;/span&gt;(x=y=0,i=99;--i/y;x=t)t=x*x-y*y+1-k%3,y=1-k/42+y*x*2&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And here’s what it renders:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Mandelbrot_23D68E9F.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Mandelbrot" border="0" alt="Mandelbrot" src="http://weblogs.asp.net/blogs/bleroy/Mandelbrot_thumb_286C9959.png" width="504" height="368" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/KiniK/statuses/2575582146"&gt;http://twitter.com/KiniK/statuses/2575582146&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7145174" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/TwitCode/default.aspx">TwitCode</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Twitter/default.aspx">Twitter</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Why is ASP.NET encoding &amp;’s in script URLs? A tale of looking at entirely the wrong place for a cause to a non-existing bug.</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/05/why-is-asp-net-encoding-amp-s-in-script-urls-a-tale-of-looking-at-entirely-the-wrong-place-for-a-cause-to-a-non-existing-bug.aspx</link><pubDate>Sat, 06 Jun 2009 00:27:15 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7109236</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7109236</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/05/why-is-asp-net-encoding-amp-s-in-script-urls-a-tale-of-looking-at-entirely-the-wrong-place-for-a-cause-to-a-non-existing-bug.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Bug_50FD5922.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Bug_thumb_774F16A0.jpg" width="254" height="184" /&gt;&lt;/a&gt; Several people have &lt;a href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134"&gt;reported&lt;/a&gt; seeing errors in their logs that seem to be due to requests such as this:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;/ScriptResource.axd?d=     &lt;br /&gt;[lots of junk]&lt;strong&gt;&amp;amp;amp;       &lt;br /&gt;&lt;/strong&gt;t=ffffffffee24147c&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The important part here is the HTML-encoded “&amp;amp;amp;” sequence, which stands for “&amp;amp;” of course. &lt;em&gt;If&lt;/em&gt; this exact URL is sent to the server, the server won’t know what to do with the escape sequence (URLs are not supposed to be HTML-encoded on the wire) so the parameters won’t get separated as expected, potentially resulting in a server error. This bug in the toolkit is an example of that: &lt;a href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134"&gt;http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Of course, when people see 500 errors popping up in their server logs, they immediately assume the application is failing for some users. Or that some idiot at Microsoft did something incredibly stupid (that’s what we idiots at Microsoft do after all).&lt;/p&gt;  &lt;p&gt;Case in point, a quick peek into the source code of the application’s pages immediately reveals that the script tags generated by ScriptManager do indeed generate these URLs:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;lt;script src=&amp;quot;/ScriptResource.axd?d=[lots of junk]&amp;amp;amp;t=ffffffff8824ac28&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;So that’s where it came from! See? &lt;strong&gt;When I copy this URL into the browser’s URL bar, I do get the same error!&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Then ensue various more or less rational reactions such as:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Correlate the user agent to the faulty requests (which correlates more or less with normal browser usage, i.e. lots of IE and then lots of Firefox, when there is a large enough sample).&lt;/li&gt;    &lt;li&gt;Blame IE6 (lots of these requests come from IE6, hence it must be responsible: IE6 sucks).&lt;/li&gt;    &lt;li&gt;“Fix” ScriptManager and remove the HTML encoding.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Well, by copying the URL from the source view into the URL bar, you did indeed reproduce the problem. A little too well. Better than you realize.&lt;/p&gt;  &lt;p&gt;This is why. &lt;strong&gt;All of the errors in your server logs come from people doing precisely what you just did&lt;/strong&gt;: copy the URL from the source view into the browser’s URL bar. They do it for various reasons: look at the source code for the scripts, understand what these weird URLs are, who knows?&lt;/p&gt;  &lt;p&gt;But the point is, you will never be able to reproduce these errors during normal use of the application. There is nothing to fix here. The value that gets sent to the server never has the “&amp;amp;amp;” sequence. You can verify it in IE6, you can verify it in any browser on any OS, it will just work.&lt;/p&gt;  &lt;p&gt;When putting a URL in an HTML attribute, you should &lt;strong&gt;*always*&lt;/strong&gt; HTML-encode it. It’s the standard, and for good reason (it enables the browser to tell between “&amp;amp;”, “&amp;amp;amp;” and “&amp;amp;amp;amp;”, it enables quotes to be embedded into attributes, etc.).&lt;/p&gt;  &lt;p&gt;A consequence of that is that if you’re going to copy the value of one of these attributes from the source view, you should do what the browser does when parsing the HTML: decode the value first (in other words, replace “&amp;amp;amp;” with “&amp;amp;”).&lt;/p&gt;  &lt;p&gt;So yes, people do fail to do that and copy the URL without decoding. Well, they are not supposed to do that, nor do they need to do it. The error is normal, it results from a bad URL having been entered manually. Nobody would be surprised to get an error when querying foo.aspx?somenumber=thisisnotanumber for example. Same thing here. Pretty much.&lt;/p&gt;  &lt;p&gt;Of course, this is not entirely trivial to figure out and I did pull my remaining hair a bit trying to understand what was going on, and you tend to trust people when they tell you there is a problem, especially when the description seems to make sense. There is some sort of confirmation bias going on there. But the more I looked at the different pieces of evidence, the more this explanation looked like the most likely, by far.&lt;/p&gt;  &lt;p&gt;But of course, I may be missing something…&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7109236" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category></item><item><title>Survey: Ajax usage among .NET developers</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx</link><pubDate>Fri, 22 May 2009 19:13:20 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7094884</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7094884</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Copenhagen_36DBEC47.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Copenhagen_thumb_55B26025.jpg" width="204" height="154" /&gt;&lt;/a&gt; If you haven’t already and you are a .NET developer, please take a couple minutes and answer this survey, whether you use Ajax or not. There are a number of Ajax surveys around, but Simone’s is the only one that focuses on .NET developers.&lt;/p&gt;  &lt;p&gt;The survey:   &lt;br /&gt;&lt;a title="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H" href="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H"&gt;http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Simone’s post:   &lt;br /&gt;&lt;a href="http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx"&gt;http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7094884" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx</link><pubDate>Fri, 15 May 2009 05:04:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7087574</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7087574</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="bleroy05" border="0" alt="bleroy05" align="left" src="http://weblogs.asp.net/blogs/bleroy/bleroy05_7555BBC5.jpg" width="244" height="164" /&gt; JavaScript has two ways of delaying execution of code: &lt;a href="http://msdn.microsoft.com/en-us/library/ms536749(VS.85).aspx"&gt;setInterval&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx"&gt;setTimeout&lt;/a&gt;. Both take a function or a string as the first parameter, and a number of milliseconds as the second parameter. The only difference is that the code provided to setInterval will run every n milliseconds whereas the code in &lt;a href="http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx"&gt;setTimeout&lt;/a&gt; will run only once.&lt;/p&gt;  &lt;p&gt;Before I explain why I think setInterval is evil, allow me to rant on a related subject for a paragraph: you should never pass a string into any of those functions and instead always pass a function reference (unless you really, really know what you’re doing). If you pass a string, it will have to be evaluated on the fly, and eval is quite evil itself (unless you really, really know what you’re doing). It might seem tempting to generate code this way to inject dynamic parameters, but there are better ways of doing that, using &lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;currying&lt;/a&gt;. In Microsoft Ajax, for example, we provide the handy &lt;a href="http://weblogs.asp.net/bleroy/archive/2007/04/10/how-to-keep-some-context-attached-to-a-javascript-event-handler.aspx"&gt;Function.createCallback and Function.createDelegate&lt;/a&gt; for exactly that usage.&lt;/p&gt;  &lt;p&gt;Anyways now let’s get back to the issue at hand: setInterval is evil. To better compare, let’s assume you have a reason to use setInterval (why else would you be reading this?). Here’s the code you might write:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;setInterval&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
        var &lt;/span&gt;interval = setInterval(appendDateToBody, 5000);&lt;br /&gt;&lt;span style="color: blue"&gt; &lt;br /&gt;        function &lt;/span&gt;appendDateToBody() {
            document.body.appendChild(
                document.createTextNode(&lt;span style="color: blue"&gt;new &lt;/span&gt;Date() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;));
        }

        &lt;span style="color: blue"&gt;function &lt;/span&gt;stopInterval() {
            clearInterval(interval);
        }
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Stop&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;stopInterval();&amp;quot; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Both APIs have a corresponding clear API that enables the developer to cancel the interval or timeout. To identify what interval or timeout you’re cancelling, you pass into the API a token that is whatever value the call to setInterval or setTimeout returned. And that’s a first reason why setInterval is (mildly) evil: if you lose that token, there is no way you can ever stop that code from running every five seconds until you navigate away from the page. But that is a minor inconvenience, it just means you need to carefully manage your own stuff, right? Well, actually if code that you don’t know or don’t control created that interval, you’re probably in trouble even if you kept your own house real nice and tidy…&lt;/p&gt;

&lt;p&gt;But the real reason why I dislike setInterval is that it is quite hard to debug, in particular if you have more than one running simultaneously. For example, if you have two intervals, one running every 100 milliseconds, the other every five seconds, and if you want to debug the second one, the first one will constantly get triggered and will get in the way.&lt;/p&gt;

&lt;p&gt;So what, you might say, is the alternative? Well, here is code that is quasi-equivalent to the code above, but that uses the much less evil setTimeout:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;interval = setTimeout(appendDateToBody, 5000);

&lt;span style="color: blue"&gt;function &lt;/span&gt;appendDateToBody() {
    document.body.appendChild(
        document.createTextNode(&lt;span style="color: blue"&gt;new &lt;/span&gt;Date() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;));
    interval = setTimeout(appendDateToBody, 5000);
}

&lt;span style="color: blue"&gt;function &lt;/span&gt;stopInterval() {
    clearTimeout(interval);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here, instead of taking a subscription for life, we’re just renewing the lease as we go. The big advantage of this code is that to stop the interval, you don’t need the token. Actually, I rarely even bother to keep hold of it. All you have to do is skip the line of code that renews the timeout for the next iteration. So no housekeeping to do, and if during a debugging session you need to get one of the interval functions out of the way, just drag your debugger’s current execution pointer to the end of the function, hit F5 and you’ll never see that function run ever again, boom, it’s out of the way and you can focus on your own debugging.&lt;/p&gt;

&lt;p&gt;So to summarize, replacing setInterval with setTimeout is easy, pain-free and removes the inconveniences of setInterval. So while the setInterval evil is about at the level of not replacing the cap of the toothpaste, the non-evil alternative is so easy that I can’t see a reason not to forget about setInterval for good.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7087574" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>New release of the Ajax Control Toolkit</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx</link><pubDate>Thu, 14 May 2009 05:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7086151</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>50</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7086151</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx#comments</comments><description>&lt;p&gt;A &lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;new version of the AJAX Control Toolkit&lt;/a&gt; is now available for download from the CodePlex website. It contains three new controls:&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="HtmlEditor" border="0" alt="HtmlEditor" align="left" src="http://weblogs.asp.net/blogs/bleroy/HtmlEditor_1E6A1BAC.png" width="244" height="159" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/HTMLEditor/HTMLEditor.aspx"&gt;HTMLEditor&lt;/a&gt;&lt;/strong&gt; - allows you to easily create and edit HTML content. You can edit in WYSIWYG mode or in HTML source mode. The control exists as a server-side extender but can also be instantiated purely on the client-side with a single line of code. Many thanks to &lt;a href="http://www.obout.com"&gt;Obout&lt;/a&gt; for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="ComboBox" border="0" alt="ComboBox" align="right" src="http://weblogs.asp.net/blogs/bleroy/ComboBox_0834005A.png" width="162" height="244" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx"&gt;ComboBox&lt;/a&gt;&lt;/strong&gt; - provides a DropDownList of items, combined with TextBox. Different modes determine the interplay between the text entry and the list of items. this control behaves very much like a Windows combo. Many thanks to Dan Ludwig for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ColorPicker" border="0" alt="ColorPicker" align="left" src="http://weblogs.asp.net/blogs/bleroy/ColorPicker_23002666.png" width="238" height="189" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ColorPicker/ColorPicker.aspx"&gt;ColorPicker&lt;/a&gt;&lt;/strong&gt; - can be attached to any ASP.NET TextBox control to provide client-side color-picking functionality from a popup. Many thanks to Alexander Turlov for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;The ASP.NET website has been updated with new &lt;a href="http://www.asp.net/learn/ajax-videos"&gt;videos&lt;/a&gt; and &lt;a href="http://www.asp.net/learn/ajax"&gt;tutorials&lt;/a&gt; for these controls.&lt;/p&gt;  &lt;p&gt;This new release also includes fixes for over 20 of the most voted bugs in existing AJAX Control Toolkit controls and now features minimized release versions of the script files.&lt;/p&gt;  &lt;p&gt;The release can be downloaded as a server dll or as a set of files for use with pure client-side applications.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7086151" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Creating jQuery plug-ins from MicrosoftAjax components</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx</link><pubDate>Tue, 05 May 2009 05:23:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7073206</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7073206</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy" border="0" alt="(c) Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/ConjonctionRetouche_3337CDB8.jpg" width="244" height="156" /&gt; We had an interesting discussion recently on the &lt;a href="http://aspinsiders.com/"&gt;ASP Insiders&lt;/a&gt; mailing list and ended up talking about what cool stuff we could build on top of jQuery. Many interesting things were mentioned and it was a very useful discussion but one suggestion in particular struck my curiosity as it was something I had investigated before and that could be improved on with very little code.&lt;/p&gt;  &lt;p&gt;I had already written &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/09/28/jquery-now-officially-part-of-the-net-developer-s-toolbox.aspx"&gt;a little plugin to enable instantiation of Microsoft Ajax components on the results of a jQuery selector&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;jQuery.fn.create = &lt;span style="color: blue"&gt;function&lt;/span&gt;(type, properties) {
&lt;span style="color: blue"&gt;    return this&lt;/span&gt;.each(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
        Sys.Component.create(type, properties, {}, {},&lt;span style="color: blue"&gt; this&lt;/span&gt;);
    });
};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I have another version that is a little more elaborate and takes a bag of properties and events instead of just properties (get it from the attached sample) but you get the idea. This makes it fairly easy to instantiate components based on a selector:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;)
    .create(Bleroy.Sample.CharCount, { maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;But if this were a native jQuery plugin instead of a Microsoft Ajax component, as &lt;a href="http://west-wind.com/weblog/"&gt;Rick Strahl&lt;/a&gt; suggested, chances are you’d do something like this instead:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;).charCount({ maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We can actually get that exact result fairly easily by writing a plugin that is specific to this component, but we can’t expect every Microsoft Ajax component author to also write a jQuery plugin, can we? We can do better than that. Here is a second small piece of code that is still generic but is in the business of &lt;em&gt;creating jQuery plugins&lt;/em&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.Component.exposeTojQuery = &lt;span style="color: blue"&gt;function&lt;/span&gt;(type, pluginName) {
    &lt;span style="color: blue"&gt;return &lt;/span&gt;jQuery.fn[pluginName] = &lt;span style="color: blue"&gt;function&lt;/span&gt;(properties) {
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.each(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
            Sys.Component.create(type, properties, {}, {}, &lt;span style="color: blue"&gt;this&lt;/span&gt;);
        });
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Like above, the version in the sample is better than that in that it supports events in addition to properties but again you get the idea. Thanks to this function, we can create a jQuery plugin for any existing Microsoft Ajax component with a single line of code:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.Component.exposeTojQuery(Bleroy.Sample.CharCount, &lt;span style="color: #a31515"&gt;&amp;quot;charCount&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;After this call, we can write exactly the code we wrote before, but without having had to write a specific plugin. We can then use charCount like a native jQuery plugin:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;).charCount({ maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is really easy to use, and I quite like it. We could for example add the following line of code to the new MicrosoftAjaxTemplates.js:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(jQuery)&lt;br /&gt;    Sys.Component.exposeTojQuery(Sys.UI.DataView, &lt;span style="color: #a31515"&gt;&amp;quot;dataView&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This would enable us to instantiate DataView controls as simply as this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dv&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{{ $dataItem }}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
&lt;/span&gt;    $(&lt;span style="color: #a31515"&gt;&amp;quot;.dv&amp;quot;&lt;/span&gt;).dataView({ data: [&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;baz&amp;quot;&lt;/span&gt;] });
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Get the code from here (you need to include &lt;a href="http://docs.jquery.com/Downloading_jQuery"&gt;jquery-1.3.2.js&lt;/a&gt; to the script folder):

  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7073206" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>A blog on Microsoft Ajax client templates and data</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/21/a-blog-on-microsoft-ajax-client-templates-and-data.aspx</link><pubDate>Tue, 21 Apr 2009 21:45:01 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7054330</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7054330</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/21/a-blog-on-microsoft-ajax-client-templates-and-data.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://politian.wordpress.com/"&gt;Politian&lt;/a&gt; has a &lt;a href="http://politian.wordpress.com/"&gt;great blog series&lt;/a&gt; where he goes into the details of building a data-driven Ajax application using &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;the new 4.0 client templates and data&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Check it out!   &lt;br /&gt;&lt;a href="http://politian.wordpress.com/"&gt;http://politian.wordpress.com/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7054330" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>asp:menu fix for IE8 problem available</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/23/asp-menu-fix-for-ie8-problem-available.aspx</link><pubDate>Mon, 23 Mar 2009 21:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6995426</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>62</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6995426</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/23/asp-menu-fix-for-ie8-problem-available.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2003 Bertrand Le Roy" border="0" alt="(c) 2003 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/dscn22991_47AB4CCA.jpg" width="244" height="184" /&gt; Internet Explorer 8 is a unique release in the history of Internet Explorer in more than one way, but the decision to make standards mode the default means that authors of existing sites are impacted by it, if only to set the compatibility mode to IE7.&lt;/p&gt;  &lt;p&gt;But what if your site is built using components that render out markup and script over which you have little control, such as ASP.NET WebControls? Well, if one of the controls fails in IE8 standards mode, you need to either switch to compatibility mode (ouch!) or you need the component developer to ship an updated version.&lt;/p&gt;  &lt;p&gt;During the whole IE8 development cycle, we monitored the behavior of existing controls. Most ASP.NET built-in controls have been doing just fine in IE8, or the faulty behavior was actually due to an IE bug that we reported and that got fixed.&lt;/p&gt;  &lt;p&gt;All except asp:menu. It so happens that the menu control is making a bad assumption on what the default value for z-index should be. We debated this at length with the IE team, but it became clear as we did so that they were right and that we were wrong. We had to fix that.&lt;/p&gt;  &lt;p&gt;So here it is, the patch for menu is out and you can apply it to build IE8-compatible ASP.NET WebForms sites…&lt;/p&gt;  &lt;p&gt;Windows 2000, XP, Server 2003:   &lt;br /&gt;&lt;a href="http://code.msdn.microsoft.com/KB962351/Release/ProjectReleases.aspx?ReleaseId=2294"&gt;http://code.msdn.microsoft.com/KB962351/Release/ProjectReleases.aspx?ReleaseId=2294&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Windows Vista, Server 2008:   &lt;br /&gt;&lt;a href="http://code.msdn.microsoft.com/KB967535/Release/ProjectReleases.aspx?ReleaseId=2328"&gt;http://code.msdn.microsoft.com/KB967535/Release/ProjectReleases.aspx?ReleaseId=2328&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;UPDATE:&lt;/b&gt; the KB article can now be found here:&lt;br/&gt;&lt;a href="http://support.microsoft.com/kb/962351"&gt;http://support.microsoft.com/kb/962351&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6995426" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft/default.aspx">Microsoft</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category></item></channel></rss>