Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <!-- saved from url=(0014)about:internet -->
  3. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">
  4. <title> ITU ajax </title>
  5.  
  6. <style type="text/css">
  7. div, input { margin: 10px; }
  8. </style>
  9. </head>
  10. <body>
  11.  
  12. <div id="chatArea" style="height: 300px; border: solid #aaa 1px; overflow:auto;"></div>
  13.  
  14. <form onsubmit="return uploadData()">
  15. <input type="text" id="newMessageString">
  16. <input type="submit" value="send">
  17. </form>
  18.  
  19. <div id="status" style="border: solid #aaa 1px; ">&nbsp;</div>
  20.  
  21.  
  22. <script type="text/javascript">
  23.  
  24.  
  25. /***
  26. * XMLHttpRequest object constructor (for compatibility with various browsers)
  27. */
  28.  
  29. function createXmlHttpRequestObject() {
  30. var xmlHttp;
  31. try {
  32. xmlHttp = new XMLHttpRequest(); //should work on all browsers except IE6 or older
  33. } catch (e) {
  34. try {
  35. xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); //browser is IE6 or older
  36. } catch (e) {
  37. // ignore error
  38. }
  39. }
  40. if (!xmlHttp) {
  41. alert ("Error creating the XMLHttpRequest object.");
  42. } else {
  43. return xmlHttp;
  44. }
  45. }
  46.  
  47. function uploadData() {
  48. document.getElementById("status").innerHTML = "uploadData()";
  49.  
  50. var message = document.getElementById("newMessageString").value;
  51.  
  52. try {
  53. var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject
  54.  
  55. var params = "data=" + message;
  56. xmlHttp.open("POST", "http://www.stud.fit.vutbr.cz/~xmlich02/itu-ajax/api.php" , true);
  57. xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
  58. xmlHttp.onreadystatechange = downloadData;
  59. xmlHttp.send(params);
  60.  
  61. } catch (e) {
  62. alert(e.toString());
  63. }
  64.  
  65. document.getElementById("newMessageString").value = "";
  66.  
  67. return false; // to avoid default form submit behavior
  68. }
  69.  
  70. function downloadData() {
  71. document.getElementById("status").innerHTML = "downloadData()";
  72. var last_id = 0;
  73. var xmlHttp = createXmlHttpRequestObject();
  74. xmlHttp.open("GET", "http://www.stud.fit.vutbr.cz/~xmlich02/itu-ajax/api.php", true);
  75. xmlHttp.onreadystatechange = mojeFunkceProZpracovaniPozadavku;
  76.  
  77. function mojeFunkceProZpracovaniPozadavku() {
  78. if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) { //process is completed and http status is OK
  79. var bar = document.getElementById("chatArea");
  80. var pole = JSON.parse(xmlHttp.responseText);
  81. var start = false;
  82. for (var i in pole) {
  83. if (pole[i].id > last_id) {
  84. last_id = pole[i].id;
  85. bar.innerHTML += "<b>" + pole[i].login + "</b>: " + pole[i].cnt + '<br>';
  86. }
  87. }
  88. }
  89. };
  90. xmlHttp.send(null);
  91.  
  92. }
  93. //// put your code here
  94.  
  95. downloadData();
  96. setInterval(downloadData, 3000);
  97. </script>
  98.  
  99. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement