-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
36 lines (29 loc) · 1.07 KB
/
main.js
File metadata and controls
36 lines (29 loc) · 1.07 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
const apiKey = "0b4a4c07fb09b7c4eb6d176cadf23ff5"; // <-- اینو با کلید API خودت جایگزین کن
const button = document.getElementById("searchBtn");
const resultDiv = document.getElementById("result");
button.addEventListener("click", async function () {
const city = document.getElementById("cityInput").value.trim();
if (!city) {
resultDiv.innerHTML = "Please enter a city name.";
return;
}
resultDiv.innerHTML = "Loading..."; // حالت لودینگ
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`,
);
if (!response.ok) {
throw new Error("City not found");
}
const data = await response.json();
resultDiv.innerHTML = `
<h2>${data.name}, ${data.sys.country}</h2>
<p>Temperature: ${data.main.temp} °C</p>
<p>Weather: ${data.weather[0].description}</p>
<p>Humidity: ${data.main.humidity}%</p>
<p>Wind Speed: ${data.wind.speed} m/s</p>
`;
} catch (error) {
resultDiv.innerHTML = error.message;
}
});