Friday, July 9, 2010

Prototype JavaScript framework - Light weight framework





Redesigning websites for Rich user interface may involve significant changes not only to the UI layer but also the server side programming . Programmers and Designers often find it as a big project to do a total redesign UI .

If you already have a website with lot stylesheets and javascript's change can be substantial . Prototype (http://www.prototypejs.org/) can be actually help take the first step , as its a light weight and provides a lot of utilities .

Here is an example of a project that was using vanilla AJAX and moving to prototype was very simple

Old Code

function loadXMLDocs(object)
{

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
var xmlUrl = 'search.do';
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",xmlUrl,true)
xmlhttp.send()
}
}

function xmlhttpChange()
{

if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200)
{
var xmlDoc=xmlhttp.responseXML
}
}
}


New Code after including prototype



function loadXMLDocs(object)
{

var myAjax = new Ajax.Request(url,{ method: 'post', parameters: param, onComplete: ccxmlhttpChange });
}

function xmlhttpChange()
{

var xmlDoc=originalRequest.responseXML
}




Notice the considerable reduction in boiler plate code . Also using a framework like prototype gives a added advantage of using more options -
http://www.prototypejs.org/api/ajax/options

My testing showed that the Performance was actually better than using vanilla ajax and our team is more inclined to use it .