-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (54 loc) · 2.32 KB
/
script.js
File metadata and controls
64 lines (54 loc) · 2.32 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
console.log("welcome to weather app");
var mq = window.matchMedia("(max-width: 450px)");
let loading = document.getElementById("loading");
let display=document.getElementById("display")
if (mq.matches) {
document.getElementById("bannerImage").src="bg4.jpg"
}
document.getElementById("btn").addEventListener("click", () => {
if (mq.matches) {
document.getElementById("container").style.height = "53vh";
document.getElementById("container").style.marginTop = "20px";
loading.style.margin="0 100px"
}
loading.innerText="LOADING...."
let cityName = document.getElementById("text").value;
document.getElementById("container").style.height = "20vh";
loading.style.display="inline-block"
display.style.display="none"
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=20c17fa9fcf1314d7ca2460a18f8ed82`)
.then((response) => response.json())
.then((data) => {
let city = data["name"];
if(data["message"] || cityName.toUpperCase()!=city.toUpperCase()){
loading.innerText="WRONG CITY NAME"
}
else{
let temp = data["main"]["temp"];
let info2 = data["weather"][0]["description"];
let icon = data["weather"][0]["icon"];
let humidity = data["main"]["humidity"];
let speed = data["wind"]["speed"];
var mq = window.matchMedia("(max-width: 450px)");
if (mq.matches) {
document.getElementById("container").style.height = "53vh";
}
else{
document.getElementById("container").style.height = "64vh";
}
loading.style.display="none"
display.style.display="inline-block"
let tempC=Math.round(temp-273.15)
document.getElementById("city").innerText = city.toUpperCase();
document.getElementById("temp").innerText = tempC+"°C";
document.getElementById("image").src = `https://openweathermap.org/img/wn/${icon}@2x.png`;
document.getElementById("info2").innerText =info2.toUpperCase();
document.getElementById("humidity").innerText = "HUMIDITY: "+humidity+"%";
document.getElementById("speed").innerText = "WIND SPEED: "+speed+"KM/HR";
}
})
});
function loaded() {
document.getElementById("text").value = "";
}
window.onload = loaded;