Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Testing Ajax</title>
  5.         <link rel="shortcut icon" href="aku.jpg"/>
  6.     <link rel="stylesheet" href="home.css">
  7.  
  8. </head>
  9. <header>
  10.             <div class="nav">
  11.                 <a href="index.html">Home</a> |  
  12.                 <a href="Tugas 8.html">Kategori</a> |
  13.                 <a href="riltaim.html">RealTime</a> |
  14.                 <a href="timeline.html">TimeLine</a>
  15.             </div>
  16.         </header>
  17.             <br>
  18.             <br>
  19.             <br>
  20.             <br>
  21.             <br>
  22.             <br>
  23.         <div class="isi1">
  24.  
  25. <body onload="loadDoc('http://api.babyhanana.com/index.php/home/pesan', 'chat', 7); loadDoc('http://api.babyhanana.com/index.php/home/pesan_with_id_bigger_than?last_id=0', 'timeline', 8)">
  26.  
  27. <button type="button" onclick="loadDoc('http://api.babyhanana.com/index.php/home/judul', 'konten')">Judul</button>
  28. <h1 id="konten"></h1>
  29.  
  30. <button type="button" onclick="loadDoc('http://api.babyhanana.com/index.php/home/gambar', 'gambar', 2)">Gambarnya</button>
  31. <p><img id="gambar" src=""></p>
  32.  
  33. <button type="button" onclick="loadDoc('http://api.babyhanana.com/index.php/home/kategori', 'kategori', 3)">List Kategori</button>
  34. <ul id="kategori"></ul>
  35.  
  36. <button type="button" onclick="loadDoc('http://api.babyhanana.com/index.php/home/kategori_json', 'kategori_json', 4)">Kategori</button>
  37. <ul id="kategori_json"></ul>
  38.  
  39. <button type="button" onclick="loadDoc('http://api.babyhanana.com/index.php/home/kategori_produk', 'kategori_produk', 5)">Kategori dan Produk</button>
  40. <ul id="kategori_produk"></ul>
  41.  
  42. <input type="text" id="input">
  43. <button type="button" onclick="cari()">Cari kategori</button>
  44. <ul id="cari"></ul>
  45.  
  46.  
  47.  
  48. <script>
  49.     var last_id = 0;
  50.     var konten_chat ='';
  51. function loadDoc(url, target, tipe = 1) {
  52.     var xhttp = new XMLHttpRequest();
  53.     xhttp.onreadystatechange = function() {
  54.         if (this.readyState == 4 && this.status == 200) {
  55.             if(tipe == 1) {
  56.                 document.getElementById(target).innerHTML = this.responseText;
  57.             } else if(tipe == 2) {
  58.                 document.getElementById(target).src = this.responseText;
  59.             } else if (tipe == 3) {
  60.                 var data = this.responseText;
  61.                 var data_kategori = data.split(', ');
  62.  
  63.                 document.getElementById(target).innerHTML = '';
  64.                 data_kategori.forEach(function(item){
  65.                     document.getElementById(target).innerHTML += '<li>' + item + '</li>';
  66.                 });
  67.             } else if (tipe == 4) {
  68.                 var data = this.responseText;
  69.                 var data_kategori = JSON.parse(data);
  70.  
  71.                 document.getElementById(target).innerHTML = '';
  72.                 data_kategori.forEach(function(item){
  73.                     document.getElementById(target).innerHTML += '<li>' + item + '</li>';
  74.                 });
  75.             } else if (tipe == 5) {
  76.                 var data = this.responseText;
  77.                 var data_kategori = JSON.parse(data);
  78.                 var output  = '';
  79.  
  80.                 data_kategori.forEach(function(item){
  81.                     var data_produk = item['produk'];
  82.                    
  83.                     output += '<li>';
  84.                     output += item['kategori'];
  85.                     output += '<ul>';
  86.                     data_produk.forEach(function(item_produk){
  87.                         output += '<li>' + item_produk + '</li>';
  88.                     });
  89.                     output += '</ul>';
  90.                     output += '</li>';
  91.                 });
  92.                 document.getElementById(target).innerHTML = output;
  93.             } else if (tipe == 6) {
  94.                 var data = this.responseText;
  95.                 var data_produk = JSON.parse(data);
  96.  
  97.                 if (data_produk.status) {
  98.                     document.getElementById(target).innerHTML = data_produk.status;
  99.                 } else {
  100.                     document.getElementById(target).innerHTML = '';
  101.                     data_produk.forEach(function(item){
  102.                         document.getElementById(target).innerHTML += '<li>' + item.nama + '</li>';
  103.                     });
  104.                 }
  105.             } else if(tipe == 7) {
  106.                 var data = this.responseText;
  107.                 var data_chat = JSON.parse(data);
  108.                 var output_chat = '';
  109.                 data_chat.forEach(function(chat){
  110.                     output_chat += '<li>';
  111.                     output_chat += '<small>' + chat.timestamp + '</small>';
  112.                     output_chat += '<div class="isi_pesan">' + chat.pesan + '</div>';
  113.                     output_chat += '</li>';
  114.                 });
  115.                 document.getElementById(target).innerHTML = output_chat;
  116.  
  117.                 console.log(data_chat);
  118.             } else if(tipe == 8) {
  119.                 var data = this.responseText;
  120.                 var data_chat = JSON.parse(data);
  121.  
  122.                 var output_chat_realtime    = '';
  123.                 data_chat.forEach(function(chat){
  124.                     if (parseInt(chat.id) > last_id) {
  125.                         last_id = parseInt(chat.id);
  126.                     }
  127.                     output_chat_realtime += '<li>';
  128.                     output_chat_realtime += '<small>' + chat.timestamp + '</small>';
  129.                     output_chat_realtime += '<div class="isi_pesan">' + chat.pesan + '</div>';
  130.                     output_chat_realtime += '</li>';
  131.                 });
  132.                 console.log(last_id);
  133.                 konten_chat = output_chat_realtime + konten_chat;
  134.  
  135.                 if ( konten_chat != document.getElementById(target).innerHTM) {
  136.                     document.getElementById(target).innerHTML = konten_chat;
  137.                 }
  138.                 setTimeout(function(){
  139.                     loadDoc('http://api.babyhanana.com/index.php/home/pesan_with_id_bigger_than?last_id='+last_id, 'timeline', 8)
  140.                 },
  141.                 5000);
  142.             }
  143.         }
  144.     }
  145.     xhttp.open("GET", url, true);
  146.     xhttp.send();
  147. }
  148. function cari(){
  149.     var url = "http://api.babyhanana.com/index.php/home/produk_by_kategori?kategori=";
  150.     var kategori = document.getElementById('input').value;
  151.  
  152.     loadDoc(url+kategori, 'cari', 6);
  153. }
  154. function kirim(){
  155.     var url = 'http://api.babyhanana.com/index.php/home/kirim_pesan';
  156.  
  157.     var isi = document.getElementById('pesan').value;
  158.     var parameter = 'pesan='+isi;
  159.  
  160.     var http = new XMLHttpRequest();
  161.     http.open("POST", url, true);
  162.  
  163.     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  164.  
  165.     http.onreadystatechange = function() {
  166.         if(this.readyState == 4 && this.status == 200) {
  167.             var data = JSON.parse(this.responseText);
  168.             alert(data.status);
  169.             document.getElementById('pesan').value = '';
  170.         }
  171.     }
  172.     http.send(parameter);
  173. }
  174. </script>
  175.         <footer><marquee>Copyright &copy; <strong>yunushedi</strong></marquee></footer>
  176.  
  177. </body>
  178. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement