-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
52 lines (47 loc) · 1.55 KB
/
script.js
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
var cel = 0, faren = 0;
function renderWeather(city) {
$.ajax("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&APPID=061f24cf3cde2f60644a8240302983f2").done(function(data) {
console.log(data.main["temp"]);
console.log(data["name"]);
document.getElementById("city").innerHTML = city;
cel = data.main["temp"];
faren = Math.round(cel * (9/5) + 32);
icon = "http://openweathermap.org/img/w/"+data.weather[0].icon+".png";
climate = data.weather[0].description;
$("#icon").attr("src", icon);
document.getElementById("temperature").innerHTML = cel + " ";
document.getElementById("climate").innerHTML = climate;
$('#error-info').hide();
$('#weather-info').show();
});
}
function displayLocalWeather()
{
$.ajax("http://ipinfo.io/json").done(function(data) {
renderWeather(data.city);
});
}
$(document).ready(function(){
displayLocalWeather();
celcius = 1;
$("#unit").on('click', function(){
if(celcius == 1)
{
document.getElementById("temperature").innerHTML = faren + " ";
document.getElementById("unit").innerHTML = "°F"
celcius = 0;
}
else
{
document.getElementById("temperature").innerHTML = cel + " ";
document.getElementById("unit").innerHTML = "°C"
celcius = 1
}
});
$('#search input').on('keyup', function(e) {
if(e.which == 13 && $('#search input').val() != "")
{
renderWeather($('#search input').val(), "");
}
});
});