index.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script>
  7. let countries;
  8. function getRespomce (url) {
  9. return new Promise((ok, neok) => {
  10. let request = new XMLHttpRequest();
  11. request.open('GET', url, true);
  12. request.onload = () =>{
  13. if (request.status == 200) ok(request.response);
  14. else neok(request.statusText);
  15. }
  16. request.send();
  17. });
  18. }
  19. function renderCountrySelector(data, parentNode){
  20. let select = parentNode.appendChild(document.createElement("select"));
  21. for (let value in data) select.add(new Option(value, value));
  22. select.size = 10;
  23. select.onchange = () => {
  24. renderCitySelector(select.value, parentNode);
  25. }
  26. }
  27. function renderCitySelector(data, parentNode){
  28. if (document.getElementById('citySelector')) document.body.removeChild(document.getElementById('citySelector'));
  29. let select = parentNode.appendChild(document.createElement("select"));
  30. for (let value of countries[data]) select.add(new Option(value, value));
  31. select.size = 10;
  32. select.id = 'citySelector';
  33. console.dir(parentNode)
  34. select.onchange = async () => {
  35. if (document.getElementById('weatherParagraph')) document.body.removeChild(document.getElementById('weatherParagraph'));
  36. let yql = `select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="${select.value}")`
  37. let weather = JSON.parse(await getRespomce('https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(yql) + '&format=json'))
  38. let p = parentNode.appendChild(document.createElement("p"));
  39. p.id = 'weatherParagraph'
  40. p.innerText = weather.query.results.channel.description;
  41. }
  42. }
  43. async function weather (url) {
  44. countries = JSON.parse(await getRespomce(url));
  45. renderCountrySelector(countries, document.body);
  46. }
  47. weather('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json');
  48. </script>
  49. </head>
  50. <body>
  51. </body>
  52. </html>