Creating a list Item using Javascript and webservices in SharePoint

I came across the requirement from customer to avoid postbacks while creating list item for WSS 3.0 application.

Below code is executing completly at client side by using webservice of List.asmx in Javascript,

<html>
<h2>Read File in Javascript</h2>
<script language=”JavaScript”>
var list;
var filePath;
function CreateListItem(listname)
{
  var url = “http://siteurl/_vti_bin/Lists.asmx“;
  var list = listname;
  var objTitle=document.getElementsByName(‘txtTitle’)[0];
//alert(objTitle);
//alert(objTitle.value);
  var objName=document.getElementsByName(‘txtName’)[0];
//alert(list);
  var batch = “<Batch onError=’Continue’ PreCalc=’TRUE’ ListVersion=’0′>”
  + ” <Method ID=’1′ Cmd=’New’>”
  + ” <Field Name=’ID’>New</Field>”
  + ” <Field Name=’Title’>”+objTitle.value+”</Field>”
  + ” <Field Name=’Name’>”+objName.value+”</Field>”
  + ” </Method>”
  + “</Batch>”
//alert(batch);

  var request = “<?xml version=’1.0′ encoding=’utf-8′?>”
  + “<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'&#8221;
  + ”      xmlns:xsd=’http://www.w3.org/2001/XMLSchema'&#8221;
  + ”      xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>&#8221;
  + ” <soap:Body>”
  + ” <UpdateListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’>&#8221;
  + ”       <listName>” + list + “</listName>”
  + ”       <updates>” + batch + “</updates>”
  + ” </UpdateListItems>”
  + ” </soap:Body>”
  + “</soap:Envelope>”
//alert(request);
 
//  var ws = new ActiveXObject(“Msxml2.XMLHTTP.4.0”);

  var ws = new ActiveXObject(“Microsoft.XMLHTTP”);
  ws.open(“POST”,url,false);
  ws.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
  ws.setRequestHeader(“SOAPAction”,”http://schemas.microsoft.com/sharepoint/soap/UpdateListItems“);
  ws.send(request);
alert(“Data saved successfully!”);

}

</script>

<body>
<table>
<tr>
  <td>
   First Name :  
  </td>
  <td>
   <input type=”text” name=”txtTitle”>
  </td>
</tr>
<tr>
  <td>
   Last Name :
  </td>
  <td>
   <input type=”text” name=”txtName”>
  </td>
</tr>
<tr>
  <td colspan=”2″>
   <input type=button onClick=”CreateListItem(‘TempList’)” value=”Create List Item”>
  </td>
</tr>
</table>
</body>
</html>