IE, appendChild, setAttribute, CSS Class

I want to add a star to the end of a label for mandatory fields in the DOM.

.mandatoryIndicator { font-color: red; }

 

var star = document.createElement("span");
star.setAttribute("class","mandatoryIndicator");
star.innerHTML = " *";
label.appendChild( star );

The above code doesn't work in IE, the star is added, but it remains the default text colour.

I did a bit of hunting around and found out the problem is actually with setAttribute.  It doesn't work with half a dozen properties on the element.

The fix is actually quite easy, use this:

var star = document.createElement("span");
star.className = "mandatoryIndicator";
star.innerHTML = " *";
label.appendChild( star );

Anonymous Type of an existing type

I find the .NET anonymous type a bit weak in terms of a few more features:

  1. Can't inherit/implement an existing type
    var x = new : IMyInterface {
       Number = 1,
       Name = "Hi",
    };
  2. Can't add methods
    var x = new {
       Number = 1,
       Name = "Hi",
       Action = delegate(object o){ return true; },
    }

I guess anonymous types are purely for quick throw-away data constructs.  I might just as well write a class directly with all the stuff I wanted.

Embedded WebResource

The job of a framework library is to provide a basic package of necessary services and components.

In ASP.NET, being a web application, the UI portion of the framework has to provide not only server side code, but also CSS, JavaScript and image files.

ASP.NET 2.0 made it easy with the WebResourceAttribute.  And I was hunting for a bug today:

Page.ClientScript.RegisterClientScriptResource(GetType(),
"My.Framework.Web.UI.Pages.Collapse.js");

We have this bit of code on the masterpage in the framework, as soon as we use this in applications it stops working.


We get a 404, "invalid webresource request".


The issue actually was quite easy to fix once we what the Type argument was for.  It's for ASP.NET to work out which assembly to look for the resource.


Page.ClientScript.RegisterClientScriptResource(typeof(ATypeInTheFrameworkAssembly), "My.Framework.Web.UI.Pages.Collapse.js");


Which fixed the problem.

Writing XML docs for JavaScript

One of the really nice thing about self-documenting code came from (as far as I'm aware, although they probably borrowed it from elsewhere) Java's javadoc.  In .NET this was again borrowed.

I've read a few articles recently about VS.NET 2008 inferring javascript 'doc' from javascript code, and it got me thinking.

At the end, the purpose of self-documenting code is so that fellow developers (most likely your colleagues working late or a maintainer a few years later) don't have to second-guess what a method is doing.  And you really don't want them to be calling you at 11pm in the evening.  It stuffs you up for the evening and it makes you guilty that they are still at work that late.

There are two links I've read that talks in detail how intellisense for javascript is produced from reading javascript.  I'll link them below.

http://weblogs.asp.net/scottgu/archive/2007/06/21/vs-2008-javascript-intellisense.aspx

http://weblogs.asp.net/bleroy/archive/2007/04/23/the-format-for-javascript-doc-comments.aspx 

Both are very fascinating.  Ideally javascript libraries like JQuery or Prototype (and others) should implement code commenting, as they ship with a compressed version anyway that has all the commenting stripped.

Perhaps it's possible to 'inject' code comments into jquery source code from their online API:

  1. download latest jQuery.Uncompressed
  2. grab a copy of the jQuery API
  3. for each function in jQuery js - following the rules from bleroy above, insert API
  4. happy jQuery intellisense in VS.NET 2008 :-)
  5. commit to internal source control
  6. repeat process for each new version of jQuery

jliu

Cool bits about Office Communicator

image

Our regular morning project scrum was cancelled for today, as I went back to my desk, I noticed that my Office Communicator has updated my status to "In a meeting". 

I checked my team members, theirs were updated too.

It knows this because Outlook told it about my calendar!

Another thing I liked was how you can add Active Directory groups (or possibly any outlook groups) directly as contact lists.  Which saves you the problem of adding new people (and possibly removing/maintaining your list).

That's very cool integration.  Well done MS.