Thursday, June 19, 2008

Simple Ajax Coding

oDesk Certified Software Testing Engineer

var xmlHttp // xmlHttp variable
function GetXmlHttpObject() { var objXMLHttp = null;
if( window.XMLHttpRequest ) {
try {
objXMLHttp = new XMLHttpRequest();
}
catch( e ) {
objXMLHttp = false; }
}
else if( window.createRequest ) {
try {
objXMLHttp = new window.createRequest();
}
catch( e ) {
objXMLHttp = false;
}
}
else if( window.ActiveXObject ) {
try {
objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch( e ) {
try {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch( e ) {
objXMLHttp = false;
}
}
}
return objXMLHttp;
}
function search_subcategory(){
//alert('sdfsdf');
document.getElementById('subcategory_label').style.display="none";
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null){
alert ("Browser does not support HTTP Request")
return
}
var val = document.getElementById('category').value;
var url="dropdown.php?val="+val;//alert(url);
xmlHttp.onreadystatechange = response;xmlHttp.open("GET",url,true);
xmlHttp.send(null);
function response(){
if (xmlHttp.readyState == 4) {
var x =xmlHttp.responseText;
document.getElementById('subcategory_label').style.display="block"; document.getElementById('subcategory_label').innerHTML = x; }
else {
document.getElementById(y).style.display = "block";
document.getElementById(y).innerHTML ="Loading...";
}
}
}