Tuesday, February 10, 2009

AJAX Basic Syntax

The basic framework structure of an Ajax-enabled web page. The following JavaScript shows how to send a request for an XML file and how to receive the returned request. This is the basics only you will need to refine and imlement better error handling. You will need to implement what to do with the returned XML.

AJAX Structure

<script type="text/javascript">

/* -- Function to "handle" the response -- */
function myHandler
(){
/* -- Was the request successful? -- */
if (this.readyState == 4 && this.status == 200) {
/* -- Did the request return a result? -- */
if (this.responseXML != null && this.responseXML.getElementById("stuff").firstChild.data) {
doSomething
(this.responseXML.getElementById("stuff").firstChild.data);
}
}
}

var myRequest; // Variable to hold request object

if(window.XMLHttpRequest){
myRequest =
new XMLHttpRequest(); // Standards-compliant browsers
}

else if(
window.ActiveXObject){
myRequest =
new ActiveXObject("Msxml2.XMLHTTP"); // For IE
}

myRequest.onreadystatechange
= myHandler;

/* -- "handleResponse.php" can be anything that returns an XML file -- */
myRequest.
open("GET", "handleResponse.php", true);

</script>



source : http://www.phpwebs.com

No comments: