Why I love Javascript, and Ie7 hates extra ,'s in prototyping

I've been working with javascript rather heavily for the past few... many months. Years ago when I started working with ASP.NET I was naive and thought everything should be done server side. I thought that the server should do all the work so the clients computers would be able to work fast.  It took only 1 year for me to turn that thinking around.  JavaScript rocks! 

ASP.NET AJAX works great with JavaScript.  You can point to a web service (in your current domain), get the data you need,  and do all the XHTML/HTML Markup changes necessary without a postback to the server. (yes some postbacks are evil)

So why do I love JavaScript? Aside from the Asynchronous nature of it (which can really screw with your mine sometimes), it makes the client computers do the work, and takes the load of your servers. It might be easy for your server to render a DataList of 100 rows. However, if you need that list updated every 10 seconds with the newest data, your server will start complaining when 1000-10,000 people are viewing that page.  You could cache the data, but you'll still be rendering the DataList in your presentation layer.  With JavaScript, you can create a simple object (note JavaScript is not an object based language, you use prototypes... but for the simple eye they look like objects) that can do this for you:

DataItem = function(data)
{
    this.UserID = data.UserID;
    this.UserName = data.UserName;
    this.FullName = data.FullName;
}

DataItem.prototype = {
    toString = function()
    {
        return '<div class="detail">UserID: ' + this.UserID = ', UserName: ' + this.UserName = ', FullName: ' + this.FullName = '</div>';
    },
    test = function()
    {
        var d = "test";
        return d;
    },
}

When you get the results back from your webservice, you can loop through them, creating this object (yea yea) from the data returned, and easily output the data you need.  Very simple, and you have full control. You don't need to rebuild your application if you want to change how a simple list works.

This is where I left off last night with some script I was working on. I get to the office and I began testing it again, pulling my hair out.  FireFox was working fine, however IE7 was blowing up. I kept getting "<custom object> is Undefined".  After creating a few test prototypes I found the culprit... the trailing "," in the last function of my prototype. IE is expecting there to be a new function, FireFox ignores it.

JavaScript is strict, and I love it!

3 Comments

  • I'm playing around with JSON (JavaScript Object Notation) and recently found a way to cut ASP.NET AJAX completely out of the picture for some of my needs. I'll blog about it soon.

  • Yeah ... got the same problem the first time I worked with prototyping in JS - but the funniest is - everyone who worked on that project had that problem because some guys deleted a method, some added one and copied the comma ... very funny :-D

  • @rrobins
    I love how I can cut out the ASP.NET code behind with this project. Before my thoughts on using JavaScript and ASP.NET webserviecs was "why do it". However, the performance gain because of it is insane.

    @Divi
    Sometimes those little errors like that are the most annoying things. Hard to find, and when you find them you just shake your head

Comments have been disabled for this content.