irwan

AJAX FUNDAMENTAL 2 Response to a txt file

Jul 4th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. FILE 1 : index.html
  2.  
  3. <html>
  4. <head>
  5. <title>Ajax example</title>
  6. <script language = "javascript">
  7. var XMLHttpRequestObject = false;if (window.XMLHttpRequest) {
  8. XMLHttpRequestObject = new XMLHttpRequest();
  9. } else if (window.ActiveXObject) {
  10. XMLHttpRequestObject = new
  11. ActiveXObject("Microsoft.XMLHTTP");
  12. }
  13. function getData(dataSource, divID)
  14. {
  15. if(XMLHttpRequestObject) {
  16. var obj = document.getElementById(divID);
  17. XMLHttpRequestObject.open("GET", dataSource);
  18. XMLHttpRequestObject.onreadystatechange = function()
  19. {
  20. if (XMLHttpRequestObject.readyState == 4 &&
  21. XMLHttpRequestObject.status == 200) {
  22. obj.innerHTML = XMLHttpRequestObject.responseText;
  23. }
  24. }
  25. XMLHttpRequestObject.send(null);
  26. }
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <H1>An Ajax example</H1>
  32. <form>
  33. <input type = "button" value = "Fetch the message"
  34. onclick = "getData('test.txt', 'targetDiv')">
  35. </form>
  36. <div id="targetDiv">
  37. <p>The fetched message will appear here.</p>
  38. </div>
  39. </body>
  40. </html>
  41.  
  42. FILE 2 : test.txt
  43.  
  44. test and test!
Advertisement
Add Comment
Please, Sign In to add comment