-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWheatherapi.html
More file actions
95 lines (88 loc) · 2.38 KB
/
Copy pathWheatherapi.html
File metadata and controls
95 lines (88 loc) · 2.38 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Weather App</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background: linear-gradient(to right, #6dd5ed, #2193b0);
color: white;
padding: 20px;
}
input {
padding: 10px;
font-size: 16px;
width: 200px;
border: none;
border-radius: 5px;
}
button {
padding: 10px 15px;
font-size: 16px;
margin-left: 5px;
border: none;
background: #ff9800;
color: white;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #e68900;
}
#result {
margin-top: 20px;
background: rgba(255, 255, 255, 0.2);
padding: 15px;
border-radius: 10px;
display: inline-block;
}
img {
vertical-align: middle;
}
</style>
</head>
<body>
<h1>🌦 Weather App</h1>
<input type="text" id="city" placeholder="Enter city">
<button onclick="getWeather()">Get Weather</button>
<div id="result"></div>
<script>
function getWeather() {
const city = document.getElementById("city").value.trim();
const apiKey = "4998810ba29fe970cbd63e49f8e3dc78"; // Your API key
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
if (!city) {
document.getElementById("result").innerHTML = "<p style='color:red;'>Please enter a city name</p>";
return;
}
fetch(url)
.then(response => {
if (!response.ok) {
return response.json().then(err => {
throw new Error(err.message);
});
}
return response.json();
})
.then(data => {
const temp = data.main.temp;
const description = data.weather[0].description;
const icon = data.weather[0].icon;
const iconUrl = `https://openweathermap.org/img/wn/${icon}@2x.png`;
document.getElementById("result").innerHTML = `
<h2>${data.name}</h2>
<img src="${iconUrl}" alt="${description}">
<p><strong>${temp}°C</strong> - ${description}</p>
`;
})
.catch(error => {
document.getElementById("result").innerHTML = `
<p style="color:red;">${error.message}</p>
`;
});
}
</script>
</body>
</html>