-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
113 lines (100 loc) · 2.9 KB
/
index.html
File metadata and controls
113 lines (100 loc) · 2.9 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Weather App</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: linear-gradient(to right, #83a4d4, #b6fbff);
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.weather-container {
background-color: white;
padding: 30px 40px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
max-width: 400px;
width: 100%;
}
h1 {
margin-bottom: 20px;
font-size: 28px;
}
input[type="text"] {
width: 80%;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #0077ff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-left: 10px;
}
button:hover {
background-color: #005fcc;
}
.result {
margin-top: 20px;
font-size: 20px;
}
.error {
color: red;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="weather-container">
<h1>Weather App</h1>
<input type="text" id="locationInput" placeholder="Enter location (e.g. BIHAR)" />
<button onclick="getWeather()">Get Weather</button>
<div class="result" id="result"></div>
<div class="error" id="error"></div>
</div>
<script>
async function getWeather() {
const location = document.getElementById("locationInput").value.trim();
const resultDiv = document.getElementById("result");
const errorDiv = document.getElementById("error");
resultDiv.innerHTML = "";
errorDiv.innerHTML = "";
if (!location) {
errorDiv.textContent = "Please enter a location.";
return;
}
const apiKey = "4a11d47e39d946adb67201050250508";
const url = `http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${encodeURIComponent(location)}&aqi=yes`;
try {
const response = await fetch(url);
if (!response.ok) throw new Error("Invalid location or API error");
const data = await response.json();
const tempC = data.current.temp_c;
const cityName = data.location.name;
const country = data.location.country;
resultDiv.innerHTML = `🌤️ Temperature in <strong>${cityName}, ${country}</strong>: <strong>${tempC}°C</strong>`;
} catch (error) {
errorDiv.textContent = "Could not fetch weather. Please try a valid location.";
console.error(error);
}
}
</script>
</body>
</html>