Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <!-- saved from url=(0014)about:internet -->
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">
- <title> ITU ajax </title>
- <style type="text/css">
- div, input { margin: 10px; }
- </style>
- </head>
- <body>
- <div id="chatArea" style="height: 300px; border: solid #aaa 1px; overflow:auto;"></div>
- <form onsubmit="return uploadData()">
- <input type="text" id="newMessageString">
- <input type="submit" value="send">
- </form>
- <div id="status" style="border: solid #aaa 1px; "> </div>
- <script type="text/javascript">
- /***
- * XMLHttpRequest object constructor (for compatibility with various browsers)
- */
- function createXmlHttpRequestObject() {
- var xmlHttp;
- try {
- xmlHttp = new XMLHttpRequest(); //should work on all browsers except IE6 or older
- } catch (e) {
- try {
- xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); //browser is IE6 or older
- } catch (e) {
- // ignore error
- }
- }
- if (!xmlHttp) {
- alert ("Error creating the XMLHttpRequest object.");
- } else {
- return xmlHttp;
- }
- }
- function uploadData() {
- document.getElementById("status").innerHTML = "uploadData()";
- var message = document.getElementById("newMessageString").value;
- try {
- var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject
- var params = "data=" + message;
- xmlHttp.open("POST", "http://www.stud.fit.vutbr.cz/~xmlich02/itu-ajax/api.php" , true);
- xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
- xmlHttp.onreadystatechange = downloadData;
- xmlHttp.send(params);
- } catch (e) {
- alert(e.toString());
- }
- document.getElementById("newMessageString").value = "";
- return false; // to avoid default form submit behavior
- }
- function downloadData() {
- document.getElementById("status").innerHTML = "downloadData()";
- var last_id = 0;
- var xmlHttp = createXmlHttpRequestObject();
- xmlHttp.open("GET", "http://www.stud.fit.vutbr.cz/~xmlich02/itu-ajax/api.php", true);
- xmlHttp.onreadystatechange = mojeFunkceProZpracovaniPozadavku;
- function mojeFunkceProZpracovaniPozadavku() {
- if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) { //process is completed and http status is OK
- var bar = document.getElementById("chatArea");
- var pole = JSON.parse(xmlHttp.responseText);
- var start = false;
- for (var i in pole) {
- if (pole[i].id > last_id) {
- last_id = pole[i].id;
- bar.innerHTML += "<b>" + pole[i].login + "</b>: " + pole[i].cnt + '<br>';
- }
- }
- }
- };
- xmlHttp.send(null);
- }
- //// put your code here
- downloadData();
- setInterval(downloadData, 3000);
- </script>
- </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement