-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
34 lines (30 loc) · 1.1 KB
/
script.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
let ville = "Lille";
let key = '';
function recevoirTemperature(ville) {
const url = 'https://api.openweathermap.org/data/2.5/weather?q=' + ville + '&appid=' + key + '&units=metric';
let requete = new XMLHttpRequest();
requete.open("GET", url);
requete.responseType = 'json';
requete.send();
requete.onload = function() {
if (requete.readyState === XMLHttpRequest.DONE) {
if (requete.status === 200) {
let reponse = requete.response;
let temperature = document.querySelector('#temperature_label');
temperature.textContent = reponse.main.temp;
let elementVille = document.querySelector('#ville')
elementVille.textContent = ville;
console.log(reponse);
}
else {
console.log("Une erreur est survenu, veuillez ressayez plus tard.");
}
}
}
}
recevoirTemperature(ville);
let button = document.querySelector("#changer")
button.addEventListener('click', () => {
ville = prompt("Ville ?");
recevoirTemperature(ville);
});