-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
47 lines (36 loc) · 1.24 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function getWeatherTemplate(callback) {
var a = new XMLHttpRequest(); // ajax object
// when data received
a.addEventListener("load", function () {
callback(this.responseText);
});
// what is the resource address
a.open("GET", 'template.html');
// invoke ajax request
a.send();
}
function sendToWeather(template, city) {
var url = 'weather.json';
url = 'http://api.openweathermap.org/data/2.5/weather?q='+city+'&units=metric&appid=1e8ffdb64b7d4d8dbdd4bf3a700bdeb0';
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function () {
var obj = JSON.parse(this.responseText);
template = template.replace(/{{name}}/g, obj.name);
template = template.replace('{{temp}}', obj.main.temp);
template = template.replace('{{lat}}', obj.coord.lat);
template = template.replace('{{lon}}', obj.coord.lon);
main.innerHTML += template;
console.log(template);
console.log(obj);
});
oReq.open("GET", url);
oReq.send();
}
// get the template with anonymouse function callback
getWeatherTemplate(function (t) {
// t is the html template
const cities = ['jerusalem', 'tel aviv', 'paris', 'barcelona'];
for(let i=0; i < cities.length; i++) {
sendToWeather(t, cities[i]);
}
});