Advertisement
DaniJagnjic

Untitled

Oct 16th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  2. <meta content="utf-8" http-equiv="encoding">
  3.  
  4. <html>
  5. <head>
  6. <title></title>
  7. <script>
  8.  
  9. function makeAjaxQueryFlight(){
  10.  
  11. // create an XMLHttpRequest
  12. var xhttp = new XMLHttpRequest();
  13.  
  14. // create a handler for the readyState change
  15. xhttp.onreadystatechange = function() {
  16.  
  17. readyStateChangeHandler(xhttp);
  18.  
  19. };
  20.  
  21. // get XML file by making async call
  22. xhttp.open("GET", "Question1.xml", true);
  23. xhttp.send();
  24. }
  25.  
  26. // handler for the readyState change
  27. function readyStateChangeHandler(xhttp){
  28.  
  29. if (xhttp.readyState == 4){
  30. // readyState = 4 means DONE
  31.  
  32. if(xhttp.status == 200){
  33. // status = 200 means OK
  34.  
  35. handleStatusSuccess(xhttp);
  36.  
  37. }else{
  38. // status is NOT OK
  39.  
  40. handleStatusFailure(xhttp);
  41.  
  42. }
  43. }
  44.  
  45. }
  46.  
  47. // XMLHttpRequest failed
  48. function handleStatusFailure(xhttp){
  49.  
  50. // display error message
  51.  
  52. var displayDiv = document.getElementById("display");
  53.  
  54. displayDiv.innerHTML = "XMLHttpRequest failed: status " + xhttp.status;
  55. }
  56.  
  57. // XMLHttpRequest success
  58. function handleStatusSuccess(xhttp){
  59.  
  60. var xml = xhttp.responseXML;
  61.  
  62. // parse the XML into an object
  63. var flighObj = parseXMLflightInfo(xml);
  64.  
  65. // display the object on the page
  66. displayWeather(flightInfoObj);
  67. }
  68.  
  69. // parse the XML into an object
  70. function parseXMLWeather(xml){
  71.  
  72. // print XML on the console
  73. // console.log(xml);
  74.  
  75. //create an object to hold the information in the xml file
  76. var flightInfoObj = {};
  77.  
  78. // get the flightInfo XML element
  79. var flightInfoElement = xml.getElementsByTagName("flightInfo")[0];
  80.  
  81. // get the date XML element
  82. var dateElement = flightInfoElement.getElementsByTagName("date")[0];
  83.  
  84. // get the date
  85. flightInfoObj.date = dateElement.textContent;
  86.  
  87. // get the flight XML element
  88. var flightElement = flightInfolement.getElementsByTagName("flight")[0];
  89.  
  90. // get the flight
  91. flightInfoObj.flight = flightElement.textContent;
  92.  
  93. // get the depart XML element
  94. var departElement = flightInfoElement.getElementsByTagName("depart")[0];
  95.  
  96. // get the depart
  97. flightInfoObj.humidity = departElement.textContent;
  98.  
  99. // get the arrive XML element
  100. var arriveElement = flightInfoElement.getElementsByTagName("arrive")[0];
  101.  
  102. // get the arrive
  103. flightInfoObj.wind = arriveElement.textContent;
  104.  
  105. // get the boarding XML element
  106. var boardingElement = flightInfoElement.getElementsByTagName("boarding")[0];
  107.  
  108. // get the boarding speed
  109. flightInfoObj.boarding = boardingElement.textContent;
  110.  
  111. // get the carrier XML element
  112. var carrierElement = flightInfoElement.getElementsByTagName("carrier")[0];
  113.  
  114. // get the carrier speed
  115. flightInfoObj.carrier = carrierElement.textContent;
  116.  
  117. return flightInfoObj;
  118.  
  119. }
  120.  
  121. // display the weather object on the page
  122. function displayFlightInfo(flightInfoObj){
  123. // print the weatherObj on the console
  124. // console.log(weatherObj);
  125.  
  126. // construct HTML code to display weather information
  127. var html = "<h1>" + flightInfoObj.queryLocation + "</h1>";
  128.  
  129. html = html + "<font size='5' color='gray'>" + flightInfoObj.date + "</font>";
  130. html = html + "<br /><br />";
  131.  
  132. html = html + "<font size='7'>" + flightInfoObj.flight + "</font>";
  133. html = html + "&deg;" + flightInfoObj.depart;
  134. html = html + "<br /><br />";
  135.  
  136. html = html + "<i>arrive: " + flightInfoObj.arrive + "</i>";
  137. html = html + "<br />";
  138.  
  139. html = html + "<i>boarding: " + flightInfoObj.boarding + "</i>";
  140.  
  141. html = html + "<i>carrier: " + flightInfoObj.carrier + "</i>";
  142.  
  143. // show the constructed HTML code in the display div
  144. var displayDiv = document.getElementById("display");
  145. displayDiv.innerHTML = html;
  146. }
  147.  
  148. </script>
  149. </head>
  150. <body>
  151.  
  152. <h1> Test </h1>
  153.  
  154. <button onClick="makeAjaxQueryFlight()">
  155. Test
  156. </button>
  157.  
  158. <br /><br />
  159.  
  160. <div id="display">
  161. </div>
  162.  
  163. </body>
  164. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement