nebukad

latihan-ajax2

May 14th, 2017
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Latihan Ajax</title>
  6.     <link rel="icon" type="image/x-icon" href="https://assets-cdn.github.com/favicon.ico">
  7.     <script language="javascript">
  8.         function getXMLHttpRequest() {
  9.             //jika user menggunak IE
  10.             if (window.ActiveXObject) {
  11.                 return new ActiveXObject("Microsoft.XMLHTTP");
  12.             } else if (window.XMLHttpRequest) {
  13.                 //user menggunakan browser selain IE
  14.                 return new XMLHttpRequest();
  15.             } else {
  16.                 alert("Status : can not create XMLHttpRequest Object");
  17.             }
  18.         }
  19.  
  20.         var xmlhttp = getXMLHttpRequest();
  21.  
  22.         function sendRequest(pageUrl, elementId) {
  23.             var obj = document.getElementById(elementId);
  24.             obj.innerHTML = "Loading... please wait";
  25.             if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
  26.                 xmlhttp.open("GET", pageUrl, true);
  27.                 xmlhttp.onreadystatechange = function () {
  28.                     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  29.                        var response = xmlhttp.responseText, object = JSON.parse(response);
  30.                         obj.innerHTML = response;
  31.                     }
  32.                 }
  33.                 xmlhttp.send(null);
  34.             }
  35.         }
  36.     </script>
  37.     <style>
  38.         #content {
  39.             margin-top: 20px;
  40.         }
  41.     </style>
  42. </head>
  43. <body>
  44.     <h1>Request File Menggunakan Ajax</h1>
  45.  
  46.     <button type="button" onclick="sendRequest('ajax.php','content')">
  47.         Klik disini
  48.     </button>
  49.  
  50.     <div id="content">
  51.         File request.html akan ditampilkan disini
  52.     </div>
  53. </body>
  54. </html>
Advertisement
Add Comment
Please, Sign In to add comment