Streamlining my Javascript some more...

By James at November 14, 2007 06:23
Filed Under: Web Development, Ajax and Javascript
I've been working on updating the MyUCR project at work. I'm always amazed at how much I learn, looking at my code from a year ago gives me shudders, compared to what I know now. For instance, here's a snippet of an Ajax call from last year:
var url = "ajax.aspx?getSingleMsg="+msgID + "&msgTypeID=" + getMessageType();
var xhrReq = xhrRequest("text");
xhrReq.open("GET", url, true);
xhrReq.onreadystatechange = function()
{
if(xhrReq.readyState == 4 && xhrReq.status == 200)
{
DisplaySingleMsg(xhrReq.responseText, getMessageType())
};
}
sendRequest(xhrReq);

 

Yikes! That's a heck of a lot of lines to write and maintain. I love using jQuery, but hadn't really gotten into its Ajax events. I wish I had done so earlier. Now the same code looks like this:

var url = "ajax.aspx?getSingleMsg="+msgID + "&msgTypeID=" + getMessageType();
$.get(url, function(xhr){DisplaySingleMsg(xhr, getMessageType());});

Sweet!

James

Latest crazy song in my head Les Boukakes - Bledi - L'Alawi

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About the author

James James is a five time and current Microsoft MVP in Client App Development, a Telerik Insider, a past Director on the INETA North America Board, a husband and dad, and has been developing software since the early days of Laser Discs and HyperCard stacks. As the Founder and President of the Inland Empire .NET User's Group, he has fondly watched it grow from a twice-a-month, early Saturday morning group of five in 2003, to a robust and rambunctious gathering of all types and sizes of .NET developers.

James loves to dig deep into the latest cutting edge technologies - sometimes with spectacular disasters - and spread the word about the latest and greatest bits, getting people excited about developing web sites and applications on the .NET platform, and using the best tools for the job. He tries to blog as often as he can, but usually gets distracted by EF, LINQ, MVC, ASP, SQL, XML, and most other types of acronyms. To keep calm James plays a mean Djembe and tries to practice his violin. You can follow him on twitter at @latringo.

And as usual, the comments, suggestions, writings and rants are my own, and really shouldn't reflect the opinions of my employer. That is, unless it really does.

James Twitter Feed

Recent Comments

Comment RSS

Month List