-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (100 loc) · 4.58 KB
/
Copy pathscript.js
File metadata and controls
133 lines (100 loc) · 4.58 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var searchitem = $("#formid")
var searchbar = $("#city")
var searchcity = []
var currenturl = ""
//for local storage
var citiresults = document.getElementById("citiresults")
// click event
$(searchitem).click(async function (event) {
event.preventDefault()
searchitem = searchbar.val()
currenturl = "https://api.openweathermap.org/data/2.5/weather?q=" + searchitem + " ," + searchitem + "&units=imperial¤t.temp=¤t.humidity=&daily.weather.description=¤t.uvi=&hourly.wind_speed=¤t.weather.icon=&appid=913f8a0c9bf081d9e94bfd04b9efd30c"
// get api
data = await fetch(currenturl)
.then(result => result.json())
.then(data => {
return (data)
})
weatherdisplay(data)
})
// create display for the fetch data
function weatherdisplay(data) {
var div1 = document.getElementById("W1")
var div2 = document.getElementById("W2")
var div3 = document.getElementById("W3")
var div4 = document.getElementById("W4")
var div5 = document.getElementById("W5")
var div6 = document.getElementById("W6")
var div7 = document.getElementById("W7")
var weatherIcon = document.getElementById("image")
div1.innerHTML = data.name
div2.innerHTML = "temp: " + data.main.temp
div3.innerHTML = "Max temp: " + data.main.temp_max
div4.innerHTML = "Min temp: " + data.main.temp_min
div5.innerHTML = "Wind: " + data.wind.speed
div6.innerHTML = " Humidity: " + data.main.humidity
div7.innerHTML = " UV: " + data.main.humidity
weatherIcon.src = "https://openweathermap.org/img/wn/" + data.weather[0].icon + "@2x.png"
if (div7 >= 50) {
$("#weatherdata").css('backgorund', "red")
} else
$("#weatherdata").css('background', ' white')
}
// local storage
function save(cityname) {
localStorage.setItem(city.value, city.value)
}
for (var key in localStorage) {
if (localStorage.getItem(key) != null || localStorage.getItem(key) != undefined) {
var loopresult = document.createElement("button")
loopresult.innerHTML = localStorage.getItem(key)
loopresult.value = localStorage.getItem(key)
loopresult.id = localStorage.getItem(key)
loopresult.onclick = getweather(loopresult.value)
citiresults.append(loopresult)
}
}
async function getweather(name) {
name = name.replace(' ', "%20");
currenturl = "https://api.openweathermap.org/data/2.5/weather?q=" + name + "&units=imperial¤t.temp=¤t.humidity=&daily.weather.description=¤t.uvi=&hourly.wind_speed=¤t.weather.icon=&appid=913f8a0c9bf081d9e94bfd04b9efd30c"
// get api
weatherData = null
await fetch(currenturl)
.then(result => result.json())
.then(data => {
weatherData = data
})
weatherdisplay(weatherData)
currenturl2 = "https://api.openweathermap.org/data/2.5/forecast?q=" + name + "&units=imperial¤t.temp=¤t.humidity=&daily.weather.description=¤t.uvi=&hourly.wind_speed=¤t.weather.icon=&appid=913f8a0c9bf081d9e94bfd04b9efd30c"
// get api
data2 = await fetch(currenturl2)
.then(result => result.json())
.then(data => {
return (data)
})
forecastdisplay(data2)
// display data
function forecastdisplay(data2) {
console.log(data2)
var day1 = document.getElementById("D1")
var day1icon = document.getElementById("DICON1")
var day2 = document.getElementById("D2")
var day2icon = document.getElementById("DICON2")
var day3 = document.getElementById("D3")
var day3icon = document.getElementById("DICON3")
var day4 = document.getElementById("D4")
var day4icon = document.getElementById("DICON4")
var day5 = document.getElementById("D5")
var day5icon = document.getElementById("DICON5")
day1.innerHTML = data2.list[1].main.temp
day1icon.src = "https://openweathermap.org/img/wn/" + data2.list[1].weather[0].icon + "@2x.png"
day2.innerHTML = data2.list[9].main.temp
day2icon.src = "https://openweathermap.org/img/wn/" + data2.list[9].weather[0].icon + "@2x.png"
day3.innerHTML = data2.list[17].main.temp
day3icon.src = "https://openweathermap.org/img/wn/" + data2.list[17].weather[0].icon + "@2x.png"
day4.innerHTML = data2.list[25].main.temp
day4icon.src = "https://openweathermap.org/img/wn/" + data2.list[25].weather[0].icon + "@2x.png"
day5.innerHTML = data2.list[33].main.temp
day5icon.src = "https://openweathermap.org/img/wn/" + data2.list[33].weather[0].icon + "@2x.png"
}
}