-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (32 loc) · 1.09 KB
/
index.js
File metadata and controls
36 lines (32 loc) · 1.09 KB
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
// Import stylesheets
import "./style.css";
var cityElems = Array.from(document.getElementsByClassName("città"));
for (let elem of cityElems ) {
elem.onclick = () => display(elem.innerHTML);
}
// Funzione collegata ai bottoni
// "window" necessario in StackBlitz, può essere
// omesso altrimenti
function display(city) {
var request = new XMLHttpRequest(); // Costruzione dell'oggetto "request"
// Funzione callback invocata quando la request termina
request.onload = function() {
// funzione definita arrow
if (request.status === 200) {
var dataObject = JSON.parse(request.response);
document.getElementById("risposta").innerHTML =
"A " + city + " ci sono " + dataObject.main.temp + " gradi";
} else {
document.getElementById("risposta").innerText = "Errore";
}
};
// Applico il metodo "open"
request.open(
"GET",
"https://api.openweathermap.org/data/2.5/weather?APPID=d0fda39104b3c7c45fe031a5392964c1&units=metric&q=" +
city,
true
);
// Applico il metodo send (al termine chiamerà il callback "onload")
request.send();
};