index.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Bootstrap fffCase</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  8. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  9. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  10. <script>
  11. var cityName;
  12. var mode;
  13. var metric = true;
  14. function toMetric()
  15. {
  16. document.getElementById("tempID").innerHTML = "Temperature (Celsius)";
  17. metric = true;
  18. if (mode=="currentWeather")
  19. {
  20. displayCurrentWeather();
  21. }
  22. else if(mode=="nextFiveDays")
  23. {
  24. displayNextFiveDays();
  25. }
  26. else if(mode=="hourlyWeather")
  27. {
  28. displayHourly();
  29. }
  30. }
  31. function toImperial()
  32. {
  33. document.getElementById("tempID").innerHTML = "Temperature (Fahrenheit)";
  34. metric = false;
  35. if (mode=="currentWeather")
  36. {
  37. displayCurrentWeather();
  38. }
  39. else if(mode=="nextFiveDays")
  40. {
  41. displayNextFiveDays();
  42. }
  43. else if(mode=="hourlyWeather")
  44. {
  45. displayHourly();
  46. }
  47. }
  48. function displayCurrentWeather()
  49. {
  50. var xmlDoc;
  51. var txt;
  52. mode = "currentWeather";
  53. cityName=document.getElementById("cityName").value;
  54. var xhttp = new XMLHttpRequest();
  55. xhttp.onreadystatechange = function()
  56. {
  57. if (xhttp.readyState == 4 && xhttp.status == 200)
  58. {
  59. xmlDoc= xhttp.responseXML;
  60. txt += "<tr>";
  61. txt += "<td>Today</td>";
  62. txt += "<td>" + xmlDoc.getElementsByTagName("temperature")[0].getAttribute("value") + "</td>";
  63. txt += "<td>" + xmlDoc.getElementsByTagName("speed")[0].getAttribute("value") + " " + xmlDoc.getElementsByTagName("direction")[0].getAttribute("code") + "</td>";
  64. txt += "<td>" + xmlDoc.getElementsByTagName("humidity")[0].getAttribute("value") + xmlDoc.getElementsByTagName("humidity")[0].getAttribute("unit") + "</td>";
  65. txt += "<td>" + xmlDoc.getElementsByTagName("weather")[0].getAttribute("value") + "</td>";
  66. txt += "<td>" + displayPicture(xmlDoc.getElementsByTagName("weather")[0].getAttribute("value")) + "</td>";
  67. txt += "</tr>";
  68. document.getElementById("tableID").innerHTML = txt;
  69. displayBackgroundPicture(xmlDoc.getElementsByTagName("weather")[0].getAttribute("value"));
  70. }
  71. };
  72. if(metric)
  73. {
  74. xhttp.open("GET", "https://api.openweathermap.org/data/2.5/weather?q="+cityName+"&units=metric&mode=xml&appid=a164361b43628b08cfa06c0a9fd03a02", true);
  75. xhttp.send();
  76. }
  77. else
  78. {
  79. xhttp.open("GET", "https://api.openweathermap.org/data/2.5/weather?q="+cityName+"&units=imperial&mode=xml&appid=a164361b43628b08cfa06c0a9fd03a02", true);
  80. xhttp.send();
  81. }
  82. }
  83. function displayNextFiveDays()
  84. {
  85. var xmlDoc;
  86. var txt;
  87. mode = "nextFiveDays";
  88. cityName=document.getElementById("cityName").value;
  89. var xhttp = new XMLHttpRequest();
  90. xhttp.onreadystatechange = function()
  91. {
  92. if (xhttp.readyState == 4 && xhttp.status == 200)
  93. {
  94. xmlDoc= xhttp.responseXML;
  95. var x = xmlDoc.getElementsByTagName('time');
  96. var i;
  97. for (i = 0; i < x.length; i++)
  98. {
  99. txt += "<tr>";
  100. txt += "<td>" + x[i].getAttribute('day') + "</td>";
  101. txt += "<td>" + xmlDoc.getElementsByTagName("temperature")[i].getAttribute("day") + "</td>";
  102. txt += "<td>" + xmlDoc.getElementsByTagName("windSpeed")[i].getAttribute("mps") +" "+xmlDoc.getElementsByTagName("windDirection")[i].getAttribute("code") + "</td>";
  103. txt += "<td>" + xmlDoc.getElementsByTagName("humidity")[i].getAttribute("value") + xmlDoc.getElementsByTagName("humidity")[i].getAttribute("unit") + "</td>";
  104. txt += "<td>" + xmlDoc.getElementsByTagName("symbol")[i].getAttribute("name") + "</td>";
  105. txt += "<td>" + displayPicture(xmlDoc.getElementsByTagName("symbol")[i].getAttribute("name")) + "</td>";
  106. txt += "</tr>";
  107. }
  108. document.getElementById("tableID").innerHTML = txt;
  109. }
  110. };
  111. if(metric)
  112. {
  113. xhttp.open("GET", "https://api.openweathermap.org/data/2.5/forecast?q="+cityName+"&mode=xml&units=metric&appid=a164361b43628b08cfa06c0a9fd03a02", true);
  114. xhttp.send();
  115. }
  116. else
  117. {
  118. xhttp.open("GET", "https://api.openweathermap.org/data/2.5/forecast?q="+cityName+"&mode=xml&units=imperial&appid=a164361b43628b08cfa06c0a9fd03a02", true);
  119. xhttp.send();
  120. }
  121. }
  122. function displayHourly()
  123. {
  124. var xmlDoc;
  125. var txt;
  126. mode = "hourlyWeather";
  127. cityName=document.getElementById("cityName").value;
  128. var xhttp = new XMLHttpRequest();
  129. xhttp.onreadystatechange = function()
  130. {
  131. if (xhttp.readyState == 4 && xhttp.status == 200)
  132. {
  133. xmlDoc= xhttp.responseXML;
  134. var x = xmlDoc.getElementsByTagName('time');
  135. var i;
  136. for (i = 0; i < x.length; i++)
  137. {
  138. txt += "<tr>";
  139. txt += "<td>" + x[i].getAttribute('from') + " to " + x[i].getAttribute('to') + "</td>";
  140. txt += "<td>" + xmlDoc.getElementsByTagName("temperature")[i].getAttribute("value") + "</td>";
  141. txt += "<td>"+xmlDoc.getElementsByTagName("windSpeed")[i].getAttribute("mps") + " " +xmlDoc.getElementsByTagName("windDirection")[i].getAttribute("code") + "</td>";
  142. txt += "<td>" + xmlDoc.getElementsByTagName("humidity")[i].getAttribute("value") + xmlDoc.getElementsByTagName("humidity")[i].getAttribute("unit") + "</td>";
  143. txt += "<td>" + xmlDoc.getElementsByTagName("symbol")[i].getAttribute("name") + "</td>";
  144. txt += "<td>" + displayPicture(xmlDoc.getElementsByTagName("symbol")[i].getAttribute("name")) + "</td>";
  145. txt += "</tr>";
  146. }
  147. document.getElementById("tableID").innerHTML = txt;
  148. }
  149. };
  150. if(metric)
  151. {
  152. xhttp.open("GET", "https://api.openweathermap.org/data/2.5/forecast?q="+cityName+"&mode=xml&cnt=5&appid=a164361b43628b08cfa06c0a9fd03a02&units=metric", true);
  153. xhttp.send();
  154. }
  155. else
  156. {
  157. xhttp.open("GET", "https://api.openweathermap.org/data/2.5/forecast?q="+cityName+"&mode=xml&cnt=5&appid=a164361b43628b08cfa06c0a9fd03a02&units=imperial", true);
  158. xhttp.send();
  159. }
  160. }
  161. function displayBackgroundPicture(string)
  162. {
  163. if(string.toLowerCase().indexOf('clear') >= 0)
  164. {
  165. document.body.style.backgroundImage = "url('tumblr_m6ltvk2pHg1r9bkeao1_500.gif')";
  166. document.body.style.backgroundSize = "cover";
  167. }
  168. else if(string.toLowerCase().indexOf('cloud') >= 0)
  169. {
  170. document.body.style.backgroundImage = "url('giphy.gif')";
  171. document.body.style.backgroundSize = "cover";
  172. }
  173. else if(string.toLowerCase().indexOf('rain') >= 0)
  174. {
  175. document.body.style.backgroundImage = "url('tumblr_nd36gwxSLe1si1p8jo1_500.gif')";
  176. document.body.style.backgroundSize = "cover";
  177. }
  178. else if(string.toLowerCase().indexOf('snow') >= 0)
  179. {
  180. document.body.style.backgroundImage = "url('black-and-white-gif-photography-sky-Favim.com-2334150.gif')";
  181. document.body.style.backgroundSize = "cover";
  182. }
  183. }
  184. function displayPicture(string)
  185. {
  186. if(string.toLowerCase().indexOf('clear') >= 0)
  187. {
  188. return "<img class=\"img-responsive\" src=\"ncEEjypai.gif\" alt=\"Chania\" width=\"100\" height=\"100\">";
  189. }
  190. else if(string.toLowerCase().indexOf('cloud') >= 0)
  191. {
  192. return "<img class=\"img-responsive\" src=\"cloud-clip-art-rgtaylor_csc_net_wan_cloud.png\" alt=\"Chania\" width=\"100\" height=\"100\">";
  193. }
  194. else if(string.toLowerCase().indexOf('rain') >= 0)
  195. {
  196. return "<img class=\"img-responsive\" src=\"rain-clip-art-RcdRg7eei.png\" alt=\"Chania\" width=\"100\" height=\"100\">";
  197. }
  198. else if(string.toLowerCase().indexOf('snow') >= 0)
  199. {
  200. return "<img class=\"img-responsive\" src=\"snowflakes_PNG7545.png\" alt=\"Chania\" width=\"100\" height=\"100\">";
  201. }
  202. }
  203. </script>
  204. </head>
  205. <body>
  206. <nav class="navbar navbar-inverse">
  207. <div class="container-fluid">
  208. <div class="navbar-header">
  209. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
  210. <span class="icon-bar"></span>
  211. <span class="icon-bar"></span>
  212. <span class="icon-bar"></span>
  213. </button>
  214. <a class="navbar-brand" href="#">CPS630 Lab 02 - Zhe Zeng(500523574)</a>
  215. </div>
  216. <div class="collapse navbar-collapse" id="myNavbar">
  217. <ul class="nav navbar-nav">
  218. <li><a href="#" onclick="toMetric()">Metric</a></li>
  219. <li><a href="#" onclick="toImperial()">Imperial</a></li>
  220. <li><a href="#" onclick="displayCurrentWeather()">Current Forecast</a></li>
  221. <li><a href="#" onclick="displayNextFiveDays()">Next 5 Days Forecast</a></li>
  222. <li><a href="#" onclick="displayHourly()">Hourly Forecast</a></li>
  223. </ul>
  224. </div>
  225. <div class="form-group">
  226. <input type="text" class="form-control" id="cityName" placeholder="Enter City Name Here" oninput="displayCurrentWeather()">
  227. </div>
  228. </div>
  229. </nav>
  230. <div class="container" style="background: white;">
  231. <h2>Weather Table</h2>
  232. <div class="table-responsive" >
  233. <table class="table">
  234. <thead>
  235. <tr>
  236. <th>Time</th>
  237. <th id ="tempID">Temperature (Celsius)</th>
  238. <th id ="windID">Wind</th>
  239. <th>Humidity</th>
  240. <th>Weather Condition</th>
  241. <th>Weather Picture</th>
  242. </tr>
  243. </thead>
  244. <tbody id ="tableID">
  245. </tbody>
  246. </table>
  247. </div>
  248. </div>
  249. </body>
  250. </html>