bottom
wait
Valid XHTML 1.0!
Get Firefox!
Programming tutorials - Simple ajax script (JavaScript)
index.html
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>Ajax dynamic content</title>
  5. <script type="text/javascript" src="ajax.js"></script>
  6. </head>
  7. <body onload="ajax_function('divid','page.php')">
  8. <div id="divid">Please wait...</div>
  9. </body>
  10. </html>
ajax.js
  1. function ajax_function(id, page){
  2. var xmlHttp;
  3. try{
  4. // Firefox, Opera 8.0+, Safari
  5. xmlHttp=new XMLHttpRequest();
  6. }
  7. catch (e){
  8. // Internet Explorer
  9. try{
  10. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  11. }
  12. catch (e){
  13. try{
  14. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. catch (e){
  17. alert("Your browser does not support AJAX!");
  18. return false;
  19. }
  20. }
  21. }
  22. xmlHttp.onreadystatechange=function(){
  23. if(xmlHttp.readyState==4){
  24. document.getElementById(id).innerHTML=xmlHttp.responseText;
  25. }
  26. }
  27. xmlHttp.open("GET",page,true);
  28. xmlHttp.send(null);
  29. }
page.php
  1. <?php
  2. // This is a php file
  3. phpinfo();
  4. ?>
Print this page
bottom