main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var request = new XMLHttpRequest()
  2. var selects = document.querySelector('.selects');
  3. var weatherDiv = document.querySelector('.weather');
  4. request.open('GET', 'https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json', true) // последний параметр указывает на синхронность или асинхронность запроса. В данном случае запрос асинхронный
  5. request.onreadystatechange = function(){ //обработчик изменения статуса запроса. Статус == 4 сигнализирует о том, что запрос окончен.
  6. if (request.readyState != 4){
  7. return;
  8. }
  9. if (request.status == 200){
  10. var object = JSON.parse(request.responseText);
  11. var keys = Object.keys(object);
  12. var select = document.createElement("select");
  13. for (var i = 0; i < keys.length; i++) {
  14. var option = document.createElement("option");
  15. option.innerHTML = keys[i];
  16. option.value = keys[i];
  17. select.appendChild(option);
  18. }
  19. select.onchange = function(e){
  20. console.log(this.value)
  21. var select = document.createElement("select");
  22. for (var i = 0; i < object[this.value].length; i++) {
  23. var option = document.createElement("option")
  24. option.innerHTML = object[this.value][i];
  25. select.appendChild(option);
  26. }
  27. if(selects.children.length > 1){
  28. selects.replaceChild(select, document.getElementById("main").nextElementSibling);
  29. }
  30. else
  31. selects.appendChild(select);
  32. request.open('GET', "https://query.yahooapis.com/v1/public/yql?" + 'q=' + encodeURIComponent("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'" + select.value + "\') and u='c'") + '&format=json', true);
  33. console.log(this.value)
  34. request.onreadystatechange = function(){
  35. if(request.readyState === 4 && request.status === 200){
  36. console.log(JSON.parse(request.responseText));
  37. var obj = JSON.parse(request.responseText);
  38. var weather = obj.query.results.channel.item.description;
  39. var resWeather = weather.replace(/href|CDATA|[\!]|[\[]|[\]]/g,"");
  40. var resWeather2 = resWeather.slice(1,resWeather.length - 1);
  41. var resWeather3 = resWeather2.replace(/Full Forecast at Yahoo Weather/g,"");
  42. var resWeather4 = resWeather3.slice(0,resWeather3.length - 28);
  43. weatherDiv.innerHTML = resWeather4;
  44. console.log(resWeather4)
  45. }
  46. else if(request.readyState > 1 && request.readyState < 4){
  47. weatherDiv.setAttribute("heigt", "400px")
  48. weatherDiv.innerHTML = "loading...";
  49. }
  50. else if(request.status === 400){
  51. weatherDiv.innerHTML = "error 400";
  52. }
  53. }
  54. request.send();
  55. select.onchange = function(){
  56. request.open('GET', "https://query.yahooapis.com/v1/public/yql?" + 'q=' + encodeURIComponent("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'" + this.value + "\') and u='c'") + '&format=json', true);
  57. console.log(this.value)
  58. request.onreadystatechange = function(){
  59. if(request.readyState === 4 && request.status === 200){
  60. console.log(JSON.parse(request.responseText));
  61. var obj = JSON.parse(request.responseText);
  62. var weather = obj.query.results.channel.item.description;
  63. var resWeather = weather.replace(/href|CDATA|[\!]|[\[]|[\]]/g,"");
  64. var resWeather2 = resWeather.slice(1,resWeather.length - 1);
  65. var resWeather3 = resWeather2.replace(/Full Forecast at Yahoo Weather/g,"");
  66. var resWeather4 = resWeather3.slice(0,resWeather3.length - 28);
  67. weatherDiv.innerHTML = resWeather4;
  68. console.log(resWeather4)
  69. // var resObj = obj.query.results;
  70. // var keys = Object.keys(resObj);
  71. // var table = document.createElement("table");
  72. // for (var i = 0; i < keys.length; i++) {
  73. // var tr = document.createElement("tr");
  74. // var td1 = document.createElement("td");
  75. // var td2 = document.createElement("td");
  76. // td1.innerHTML = keys[i];
  77. // td2.innerHTML = resObj[keys[i]];
  78. // tr.appendChild(td1);
  79. // tr.appendChild(td2);
  80. // table.appendChild(tr);
  81. // }
  82. // document.body.appendChild(table);
  83. }
  84. else if(request.readyState > 1 && request.readyState < 4){
  85. weatherDiv.setAttribute("heigt", "400px")
  86. weatherDiv.innerHTML = "loading...";
  87. }
  88. else if(request.status === 400){
  89. weatherDiv.innerHTML = "error 400";
  90. }
  91. }
  92. request.send();
  93. }
  94. }
  95. selects.appendChild(select);
  96. select.id = "main"
  97. }
  98. else {
  99. alert('shit happens: ' + xhr.status + ', ' + xhr.statusText );
  100. }
  101. }
  102. request.send()