Advertisement
GWibisono

ajax 003 reload

Jun 1st, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.40 KB | None | 0 0
  1. <link rel="stylesheet" type="text/css" href="css/basic.css">
  2. <h1>load automatis dibawah dan reload saat tekan tombol</h1>
  3. <p>saat halaman ini di klik. automatis dibawah ini akan ada tulisannya. Silakan view source dan lihat bahwa dibawah tidak ada tulisan! Versi ini memasukkan perintah dalam fungsi</p>
  4. <p id='contoh'>kosong disini</p>
  5. <p> <button onclick='reload()' type='button' >Click untuk reload tulisan</button></p>
  6. code ajax<pre>
  7. $.ajax({
  8.     url: "webservice",
  9.     type: "GET",
  10.     success: function(d) {
  11.         console.log(d);
  12.     }
  13. });
  14. </pre>
  15. read more : https://www.sitepoint.com/jquery-vs-raw-javascript-3-events-ajax/
  16. <script>
  17.  ajaxGetShowId('001-get.php','contoh' );
  18.  function reload(){
  19.     ajaxGetShowId('001-get.php','contoh',true,1 );
  20.  }
  21. /*
  22. Nama sementara
  23. */
  24. function ajaxGetShowId(url, targetId, async=true,debug=0){
  25. //  var r = new XMLHttpRequest();
  26.     var xhttp;
  27.     if (window.XMLHttpRequest) {
  28.         xhttp = new XMLHttpRequest();
  29.         } else {
  30.         // code for IE6, IE5
  31.         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  32.     }
  33.     if(async!=true)async=false;
  34.     xhttp.open("GET", url, async);
  35.     xhttp.onreadystatechange = function () {
  36.         if(debug==1){
  37.             console.log(xhttp);
  38.         }
  39.         if (xhttp.readyState != 4 || xhttp.status != 200){
  40.             return 0;
  41.         }
  42.         else{
  43.             document.getElementById(targetId).innerHTML=xhttp.responseText ;
  44.             //return xhttp.responseText;
  45.         }
  46.     };
  47.     xhttp.send();
  48.     return xhttp;
  49. }
  50. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement