-
-
Notifications
You must be signed in to change notification settings - Fork 944
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (29 loc) · 1.26 KB
/
script.js
File metadata and controls
31 lines (29 loc) · 1.26 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
document.addEventListener('DOMContentLoaded', () => {
const weatherInfoDiv = document.getElementById('weather-info');
const apiUrl = 'https://api.open-meteo.com/v1/forecast?latitude=50.93333&longitude=6.95¤t=temperature_2m,relative_humidity_2m,wind_speed_10m';
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data && data.current) {
const temperature = data.current.temperature_2m;
const humidity = data.current.relative_humidity_2m;
const windSpeed = data.current.wind_speed_10m;
weatherInfoDiv.innerHTML = `
<p>Temperature: ${temperature}°C</p>
<p>Humidity: ${humidity}%</p>
<p>Wind Speed: ${windSpeed} km/h</p>
`;
} else {
throw new Error('Invalid weather data format');
}
})
.catch(error => {
console.error('Error fetching weather data:', error);
weatherInfoDiv.innerHTML = '<p>Could not fetch weather data. Please try again later.</p>';
});
});