Sunday, January 9, 2011

Ajax: The Web 'amplifier

Ajax is used for dynamically changing a web-page( i.e. changing a part of the page without refreshing the whole page). Here is an example how it works:
 1st file: Example.html
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Data will be shown here</h2></div>
<button type="button" onclick="loadXMLDoc()">Load Text</button>

</body>
</html>

2nd file: data.txt
"So here it shows how it works"

This way the ajax technology works.

No comments:

Post a Comment