Spaces:
Running
Running
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| <title>Weather App</title> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| background: #87CEEB; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| height: 100vh; | |
| margin: 0; | |
| } | |
| .weather-container { | |
| background: white; | |
| padding: 20px 30px; | |
| border-radius: 10px; | |
| box-shadow: 0 0 15px rgba(0,0,0,0.2); | |
| text-align: center; | |
| width: 300px; | |
| } | |
| input[type="text"] { | |
| width: 80%; | |
| padding: 10px; | |
| font-size: 16px; | |
| border-radius: 5px; | |
| border: 1px solid #ccc; | |
| margin-bottom: 15px; | |
| } | |
| button { | |
| padding: 10px 20px; | |
| font-size: 16px; | |
| background: #007BFF; | |
| color: white; | |
| border: none; | |
| border-radius: 5px; | |
| cursor: pointer; | |
| } | |
| button:hover { | |
| background: #0056b3; | |
| } | |
| .result { | |
| margin-top: 20px; | |
| font-size: 20px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="weather-container"> | |
| <h2>Weather App</h2> | |
| <input type="text" id="location" placeholder="Enter Location (City or Zip)" /> | |
| <button onclick="getWeather()">Get Temperature</button> | |
| <div class="result" id="result"></div> | |
| </div> | |
| <script> | |
| async function getWeather() { | |
| const location = document.getElementById('location').value; | |
| const resultDiv = document.getElementById('result'); | |
| if (!location) { | |
| resultDiv.textContent = 'Please enter a location.'; | |
| return; | |
| } | |
| const apiKey = '4b0669fa25fd4d5292b93938251311'; | |
| const url = `http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${encodeURIComponent(location)}&aqi=yes`; | |
| resultDiv.textContent = 'Loading...'; | |
| try { | |
| const response = await fetch(url); | |
| if (!response.ok) { | |
| throw new Error('Location not found'); | |
| } | |
| const data = await response.json(); | |
| const tempC = data.current.temp_c; | |
| const condition = data.current.condition.text; | |
| resultDiv.innerHTML = ` | |
| <p><strong>Location:</strong> ${data.location.name}, ${data.location.region}, ${data.location.country}</p> | |
| <p><strong>Temperature:</strong> ${tempC} °C</p> | |
| <p><strong>Condition:</strong> ${condition}</p> | |
| `; | |
| } catch (error) { | |
| resultDiv.textContent = 'Could not retrieve weather data. Please check the location and try again.'; | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> | |