|
@@ -0,0 +1,95 @@
|
|
|
+//Города-страны
|
|
|
+var request = new XMLHttpRequest();
|
|
|
+request.open('GET', "https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json", true);
|
|
|
+request.send(); // инициируем запрос.
|
|
|
+request.onreadystatechange = function(){ //обработчик изменения статуса запроса. Статус == 4 сигнализирует о том, что запрос окончен
|
|
|
+ if (request.readyState != 4){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (request.status == 200){
|
|
|
+ var newObj=JSON.parse(request.responseText);
|
|
|
+
|
|
|
+ var currentCountryOption;
|
|
|
+ var country=document.getElementById("country");
|
|
|
+ var city=document.getElementById("city");
|
|
|
+
|
|
|
+ var default_country=document.createElement("option");
|
|
|
+ default_country.innerText="-Выберете страну-";
|
|
|
+ default_country.id="default_country";
|
|
|
+ default_country.selected="selected";
|
|
|
+ country.appendChild(default_country);
|
|
|
+
|
|
|
+ var default_city=document.createElement("option");
|
|
|
+ default_city.innerText="-Выберете город-";
|
|
|
+ default_country.id="default_city";
|
|
|
+ default_city.selected="selected";
|
|
|
+ city.appendChild(default_city);
|
|
|
+
|
|
|
+ var i=0;
|
|
|
+ var countryOptions=[];
|
|
|
+ for(var cntr in newObj){
|
|
|
+ countryOptions[i]=document.createElement("option");
|
|
|
+ countryOptions[i].innerText=cntr;
|
|
|
+ countryOptions[i].id="i";
|
|
|
+ country.appendChild(countryOptions[i]);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ country.onchange=function(){
|
|
|
+ currentCountryOption=country.value;
|
|
|
+ city.innerHTML="";
|
|
|
+ city.appendChild(default_city);
|
|
|
+ if(currentCountryOption=="-Выберете страну-"){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var cityOptions=[];
|
|
|
+ for(var i=0;i<newObj[currentCountryOption].length;i++){
|
|
|
+ cityOptions[i]=document.createElement("option");
|
|
|
+ cityOptions[i].innerText=newObj[currentCountryOption][i];
|
|
|
+ city.appendChild(cityOptions[i]);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ city.onchange=function(){
|
|
|
+ var YQLSelect = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + city.value.toLowerCase() +"')";
|
|
|
+ var weatherRequest = new XMLHttpRequest();
|
|
|
+ weatherRequest.open('GET', "https://query.yahooapis.com/v1/public/yql?" + 'q=' + encodeURIComponent(YQLSelect) + '&format=json', true);
|
|
|
+ weatherRequest.send();
|
|
|
+ console.log("https://query.yahooapis.com/v1/public/yql?" + 'q=' + encodeURIComponent(YQLSelect) + '&format=json');
|
|
|
+
|
|
|
+ weatherRequest.onreadystatechange = function(){
|
|
|
+ var weatherObj;
|
|
|
+ if (weatherRequest.readyState != 4){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (weatherRequest.status == 200){
|
|
|
+ weatherObj=JSON.parse(weatherRequest.responseText);
|
|
|
+ var div=document.getElementById("weather");
|
|
|
+ var divs=[];
|
|
|
+ var j=0;
|
|
|
+ for(var i=0;i<weatherObj.query.results.channel.item.forecast.length;i++){
|
|
|
+ divs[i]=document.createElement("div");
|
|
|
+ divs[i].style.margin="5px";
|
|
|
+ divs[i].style.padding="5px";
|
|
|
+ divs[i].style.border="dotted 1px gray";
|
|
|
+ j=0;
|
|
|
+ for(var key in weatherObj.query.results.channel.item.forecast[i]){
|
|
|
+ var d=document.createElement("div");
|
|
|
+ d.innerText=Object.keys(weatherObj.query.results.channel.item.forecast[i])[j]+" : "+weatherObj.query.results.channel.item.forecast[i][key];
|
|
|
+ divs[i].appendChild(d);
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ div.appendChild(divs[i]);
|
|
|
+ }
|
|
|
+ console.log(weatherObj.query.results.channel.item.forecast);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ alert('shit happens: ' + weatherRequest.status + ', ' + weatherRequest.statusText );
|
|
|
+ }
|
|
|
+ };
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ alert('shit happens: ' + request.status + ', ' + request.statusText );
|
|
|
+ }
|
|
|
+};
|