Aaj tumhara app internet se asli data layega! City ka naam likho aur uska live temperature, mausam, humidity aur hawa dikhao. Hum free Open-Meteo API use karenge — koi signup ya API key nahi chahiye.
fetch() se data mangwanaasync / await — jawab aane tak rukna, bina page atkayetry / catch — internet band ho to bhi app crash na hoencodeURIComponent — "New Delhi" jaise naam URL me safeHar step me "Editor me daalo" dabao — ya best: khud type karo, haath se likha code yaad rehta hai. ✍️
#result khaali hai — mausam JS se aayega. <div class="app">
<h1>Mausam ⛅</h1>
<form id="form">
<input id="city" placeholder="City ka naam..."
autocomplete="off">
<button>Search</button>
</form>
<div class="result" id="result"></div>
</div>
linear-gradient — raat jaisa neela 🌌body {
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
background: linear-gradient(135deg, #0f172a, #312e81);
font-family: sans-serif;
}
.app {
width: 320px;
padding: 24px;
background: #fff;
border-radius: 20px;
text-align: center;
}
h1 { margin: 0 0 16px; }
form { display: flex; gap: 8px; }
input {
flex: 1;
padding: 10px 12px;
border: 2px solid #e2e8f0;
border-radius: 10px;
}
button {
padding: 10px 16px;
border: 0;
border-radius: 10px;
background: #6366f1;
color: #fff;
cursor: pointer;
}
.result .temp — temperature bada aur bold, sabse pehle nazar aaye..result { margin-top: 16px; }
.result h2 { margin: 0; font-size: 20px; }
.result p { margin: 6px 0; color: #475569; }
.result .temp {
margin: 4px 0;
font-size: 56px;
font-weight: bold;
color: #6366f1;
}
const GEO = 'https://geocoding-api.open-meteo.com/v1/search';
const API = 'https://api.open-meteo.com/v1/forecast';
const form = document.querySelector('#form');
const input = document.querySelector('#city');
const result = document.querySelector('#result');
weather_code) me batati hai: 0 = saaf, 61 = baarish…if ki seedhi se number ko emoji + Hinglish label me badla.function getIcon(code) {
if (code === 0) return '☀️ Saaf aasmaan';
if (code <= 3) return '⛅ Thode baadal';
if (code <= 48) return '🌫️ Kohra';
if (code <= 67) return '🌧️ Baarish';
if (code <= 77) return '❄️ Barf';
if (code <= 82) return '🌦️ Bauchhar';
return '⛈️ Toofan';
}
async function ke andar await fetch(url) — jawab aane tak ruko, phir await res.json() se object banao.results nahi mila → "City nahi mili 😕".data.current se values nikaalo.catch me friendly message.async function getWeather(city) {
result.innerHTML = '<p>Loading... ⏳</p>';
try {
const q = encodeURIComponent(city);
const geoRes = await fetch(`${GEO}?name=${q}&count=1`);
const geo = await geoRes.json();
if (!geo.results) {
result.innerHTML = '<p>City nahi mili 😕</p>';
return;
}
const place = geo.results[0];
const params = `latitude=${place.latitude}`
+ `&longitude=${place.longitude}`
+ '¤t=temperature_2m,relative_humidity_2m,'
+ 'wind_speed_10m,weather_code';
const res = await fetch(`${API}?${params}`);
const data = await res.json();
const c = data.current;
result.innerHTML = `
<h2>${place.name}, ${place.country}</h2>
<p class="temp">${Math.round(c.temperature_2m)}°C</p>
<p>${getIcon(c.weather_code)}</p>
<p>💧 Humidity ${c.relative_humidity_2m}%</p>
<p>💨 Hawa ${c.wind_speed_10m} km/h</p>`;
} catch (err) {
result.innerHTML = '<p>Internet check karo 📶</p>';
}
}
preventDefault() — page reload nahi.getWeather(city).form.addEventListener('submit', (e) => {
e.preventDefault();
const city = input.value.trim();
if (city) getWeather(city);
});
Naya day aate hi reel aayegi — @codeyantra follow karo 🔔