Ponomarenko.Oleg 6 jaren geleden
bovenliggende
commit
e1e81e8fc9
2 gewijzigde bestanden met toevoegingen van 110 en 0 verwijderingen
  1. 25 0
      hw9/index.html
  2. 85 0
      hw9/main.js

+ 25 - 0
hw9/index.html

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<meta name="viewport" content="width=device-width, initial-scale=1">
+	<link rel="stylesheet" href="style.css">
+	<title>Countries Cities Weather</title>
+</head>
+<body>
+        
+
+        <select name="" id="country"></select>
+        <select name="" id="city"></select>
+       
+        <div id="weather" style="padding-top:30px; padding-left: 20px; margin-top:40px;  width:250px;"></div>
+
+
+	
+	<script src="main.js"></script>
+	
+	</script>
+</body>
+</html>
+
+

+ 85 - 0
hw9/main.js

@@ -0,0 +1,85 @@
+
+var request = new XMLHttpRequest() 
+
+request.open('GET', 'https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json', true) 
+
+
+request.onreadystatechange = function(){  
+    if (request.readyState != 4){
+        return;
+    }
+
+    var mass = JSON.parse(request.responseText);
+        console.log(mass)
+
+    var country=document.getElementById('country')
+    var city=document.getElementById('city')
+    var weather=document.getElementById('weather')
+    var weather=document.getElementById('temperature')
+
+    if (request.status == 200){
+        alert('all ok')
+    
+       
+        for(key in mass){
+            var option = document.createElement('option')
+            option.innerText = key;
+            country.appendChild(option);
+            
+        }
+        
+
+        country.onchange = function(){
+            city.innerText ='';
+
+            for(i=0; i<mass[country.value].length; i++){
+            var option2 = document.createElement('option');
+            option2.innerText = mass[country.value][i];
+            city.appendChild(option2);
+            console.log(country.value); 
+
+        city.onchange = function(){
+            var reqW = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + city.value +"') and u='c'"
+            var requestW = new XMLHttpRequest();
+            requestW.open('GET', "https://query.yahooapis.com/v1/public/yql?" + "q=" + encodeURIComponent(reqW) + "&format=json", true);
+            requestW.onreadystatechange = function(){
+                if(requestW.readyState !=4){
+                    return;
+                }
+                if(requestW.status == 200){
+                    alert('all ok2')
+                    var weatherCity = JSON.parse(requestW.responseText);
+                    console.log(weatherCity);
+
+                var weatherParam=document.getElementById('weather');
+                dayWeather = weatherCity.query.results.channel.item.forecast;
+                var day = " ";
+                console.log(weatherCity.query.results.channel.item.condition.text)
+                for(i = 0; i < dayWeather.length; i++){
+                    day += "<b>"+ dayWeather[i].day +" "+ dayWeather[i].date +"</b></br>" 
+                        + "Temperature from " + dayWeather[i].low + " to " + dayWeather[i].high + "</br></br>";                
+                } 
+                weatherParam.style.border="2px solid red"
+                weatherParam.style.backgroundColor="yellow"
+                weatherParam.innerHTML ="восход " +  weatherCity.query.results.channel.astronomy.sunrise
+                                    + "; закат " + weatherCity.query.results.channel.astronomy.sunset+ "</br></br>"
+                                    + day;
+
+                }   
+                else {
+                    alert('shit happens: ' +  requestW.status + ', ' + requestW.statusText );
+                }
+            }
+            requestW.send();
+        }
+    }
+          
+            
+        }
+    }
+  else {
+        alert('shit happens: ' +  request.status + ', ' + request.statusText );
+    }
+}
+
+request.send()