12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <script>
- let countries;
- function getRespomce (url) {
- return new Promise((ok, neok) => {
- let request = new XMLHttpRequest();
- request.open('GET', url, true);
- request.onload = () =>{
- if (request.status == 200) ok(request.response);
- else neok(request.statusText);
- }
- request.send();
- });
- }
- function renderCountrySelector(data, parentNode){
- let select = parentNode.appendChild(document.createElement("select"));
- for (let value in data) select.add(new Option(value, value));
- select.size = 10;
- select.onchange = () => {
- renderCitySelector(select.value, parentNode);
- }
- }
- function renderCitySelector(data, parentNode){
- if (document.getElementById('citySelector')) document.body.removeChild(document.getElementById('citySelector'));
- let select = parentNode.appendChild(document.createElement("select"));
- for (let value of countries[data]) select.add(new Option(value, value));
- select.size = 10;
- select.id = 'citySelector';
- console.dir(parentNode)
- select.onchange = async () => {
- if (document.getElementById('weatherParagraph')) document.body.removeChild(document.getElementById('weatherParagraph'));
- let yql = `select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="${select.value}")`
- let weather = JSON.parse(await getRespomce('https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(yql) + '&format=json'))
- let p = parentNode.appendChild(document.createElement("p"));
- p.id = 'weatherParagraph'
- p.innerText = weather.query.results.channel.description;
- }
- }
- async function weather (url) {
- countries = JSON.parse(await getRespomce(url));
- renderCountrySelector(countries, document.body);
- }
- weather('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json');
- </script>
- </head>
- <body>
- </body>
- </html>
|