-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·38 lines (33 loc) · 1.31 KB
/
app.js
File metadata and controls
executable file
·38 lines (33 loc) · 1.31 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
"use strict";
searchButton.addEventListener('click', searchWeather);
function searchWeather() {
loadingText.style.display = 'block';
weatherBox.style.display = 'none';
var cityName = searchCity.value;
if (cityName.trim().length == 0) {
return alert('Please enter a City Name');
}
var http = new XMLHttpRequest();
var apiKey = 'YOUR_KEY';
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&units=metric&appid=' + apiKey;
var method = 'GET';
http.open(method, url);
http.onreadystatechange = function() {
if (http.readyState == XMLHttpRequest.DONE && http.status === 200) {
var data = JSON.parse(http.responseText);
var weatherData = new Weather(cityName, data.weather[0].description.toUpperCase());
weatherData.temperature = data.main.temp;
updateWeather(weatherData);
} else if (http.readyState === XMLHttpRequest.DONE) {
alert('Something went wrong!');
}
};
http.send();
}
function updateWeather(weatherData) {
weatherCity.textContent = weatherData.cityName;
weatherDescription.textContent = weatherData.description;
weatherTemperature.textContent = weatherData.temperature;
loadingText.style.display = 'none';
weatherBox.style.display = 'block';
}