Advertisement
Guest User

itu-ajax

a guest
Oct 22nd, 2019
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.98 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.  
  3. <html>
  4. <head>
  5.   <title> ITU ajax </title>
  6.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7.   <style type="text/css">
  8.   div, input { margin: 10px; }
  9.   </style>
  10. </head>
  11. <body>
  12.  
  13. <div id="chatArea" style="height: 300px; border: solid #aaa 1px; overflow:auto;"></div>
  14.  
  15. <form onsubmit="return uploadData()">
  16.   <input type="text" id="newMessageString">
  17.   <input type="submit" value="send">
  18. </form>
  19.  
  20. <!--<div id="status" style="border: solid #aaa 1px; ">&nbsp;</div> -->
  21.  
  22.  
  23. <script type="text/javascript">
  24.  
  25. //GLOBALNE PREMENNE
  26. var l_msg = 0;
  27.  
  28. /***
  29.   * XMLHttpRequest object constructor (for compatibility with various browsers)
  30.   */
  31.  
  32. function createXmlHttpRequestObject() {
  33.   var xmlhttp;
  34.   try {
  35.     xmlHttp = new XMLHttpRequest(); //should work on all browsers except IE6 or older
  36.   } catch (e) {
  37.     try {
  38.       xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); //browser is IE6 or older
  39.     } catch (e) {
  40.       // ignore error
  41.     }
  42.   }
  43.   if (!xmlHttp) {
  44.     alert ("Error creating the XMLHttpRequest object.");
  45.   } else {
  46.     return xmlHttp;
  47.   }
  48. }
  49.  
  50.  
  51. function uploadData() {
  52.   //document.getElementById("status").innerHTML = "uploadData()";
  53.  
  54.   try {
  55.     var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject
  56.  
  57.     //// put your code here
  58.     var params = "data=" + document.getElementById('newMessageString').value + "&user=xmojzi08";
  59.     xmlHttp.open("POST", "http://www.stud.fit.vutbr.cz/~xmlich02/itu-ajax/api.php", true);
  60.     xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
  61.     //xmlHttp.setRequestHeader("Content-length", params.length);
  62.     //xmlHttp.setRequestHeader("Connection", "close");
  63.     xmlHttp.onreadystatechange = downloadData();
  64.     xmlHttp.send(params);
  65.  
  66.   } catch (e) {
  67.     alert(e.toString());
  68.   }
  69.  
  70.   return false; // to avoid default form submit behavior
  71. }
  72.  
  73.  
  74. function spracovanie (par) {
  75.     if ((xmlHttp.readyState==4) && (xmlHttp.status==200)) { //process is completed and http status is OK
  76.           var msgs = JSON.parse(xmlHttp.responseText);
  77.           var chat_area = document.getElementById("chatArea");
  78.  
  79.             msgs.forEach(function(par){
  80.                 if(par.id>l_msg){
  81.                         chat_area.innerHTML += "<b>" + par.login + "</b>" + " (" + par.dts + "): " + par.cnt + "<br>";
  82.                         l_msg = par.id
  83.                     }
  84.                 });
  85.  
  86.             chat_area.scrollTop = chat_area.scrollHeight;
  87.             }
  88. }
  89.  
  90. function downloadData() {
  91.   //document.getElementById("status").innerHTML = "downloadData()";
  92.  
  93.   //// put your code here
  94.  
  95.   var xmlHttp = createXmlHttpRequestObject();
  96.   xmlHttp.onreadystatechange = spracovanie;
  97.   xmlHttp.open("GET","http://www.stud.fit.vutbr.cz/~xmlich02/itu-ajax/api.php", true);
  98.   xmlHttp.send(null);
  99. }
  100.  
  101. //// aktualizacia
  102. setInterval(downloadData, 1000); // periodicky opakuje po 3 sekundách
  103.  
  104.  
  105. </script>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement