Ryan's District Boards

Computer, programming, and webmaster help , support , tips and tricks => Tutorials Zone! => Internet webmaster computer programming technology tips and tricks => XML Tutorials => Topic started by: ben2ong2 on October 06, 2006, 04:35:19 PM

Title: Creating an XMLHttpRequest Object
Post by: ben2ong2 on October 06, 2006, 04:35:19 PM
For Mozilla, Firefox, Safari, Opera, and Netscape:
var xmlhttp=new XMLHttpRequest()
For Internet Explorer:
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
Example
<script type="text/javascript">var xmlhttpfunction loadXMLDoc(url){xmlhttp=null// code for Mozilla, etc.if (window.XMLHttpRequest)  {  xmlhttp=new XMLHttpRequest()  }// code for IEelse if (window.ActiveXObject)  {  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")  }if (xmlhttp!=null)  {  xmlhttp.onreadystatechange=state_Change  xmlhttp.open("GET",url,true)  xmlhttp.send(null)  }else  {  alert("Your browser does not support XMLHTTP.")  }}function state_Change(){// if xmlhttp shows "loaded"if (xmlhttp.readyState==4)  {  // if "OK"  if (xmlhttp.status==200)    {    // ...some code here...    }  else    {    alert("Problem retrieving XML data")    }  }}</script>
Note: An important property in the example above is the onreadystatechange property. This property is an event handler which is triggered each time the state of the request changes. The states run from 0 (uninitialized) to 4 (complete). By having the function xmlhttpChange() check for the state changing, we can tell when the process is complete and continue only if it has been successful.