Skip to content

Instantly share code, notes, and snippets.

@gaspaonrocks
Created March 16, 2017 09:00
Show Gist options
  • Select an option

  • Save gaspaonrocks/d7cd19f0dd7df1b855318a14f9d3391b to your computer and use it in GitHub Desktop.

Select an option

Save gaspaonrocks/d7cd19f0dd7df1b855318a14f9d3391b to your computer and use it in GitHub Desktop.

Revisions

  1. @gaspaonrocksBis gaspaonrocksBis created this gist Mar 16, 2017.
    17 changes: 17 additions & 0 deletions weatherapp.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    body {
    display: flex;
    flex-direction: column;
    }

    div {
    background-color: grey;
    text-align: center;
    width: 50%;
    margin: auto;
    border: 2px black solid;
    }

    #input {
    background-color: #494949;
    color: white;
    }
    36 changes: 36 additions & 0 deletions weatherapp.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <title>Weather App</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="weatherapp.css" rel="stylesheet">
    </head>

    <body>
    <div id="input">
    <p> Ask for the weather in
    <input id="city" type="text" placeholder="your city">
    </input>
    <input id="fetch" type="submit" value="FETCH !"></input>
    </p>
    <button id="reset" type="reset">AGAIN !</button>
    </div>
    <div class="display" id="title">
    <p>The weather in your city :</p>
    </div>
    <div class="display" id="temp">
    <p>La température actuelle est de</p>
    </div>
    <div class="display" id="sky">
    <p>Regardez le ciel !</p>
    <img src="" id="icon">
    </div>


    <script src="weatherapp.js">
    </script>
    </body>

    </html>
    71 changes: 71 additions & 0 deletions weatherapp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    window.addEventListener("DOMContentLoaded", function () {

    const body = document.getElementsByTagName("body")[0];

    function displayWeather(data, city) {
    var div = document.getElementById("title");
    var pNb = div.getElementsByTagName("p");
    if (pNb.length == 2) {
    return;
    } else {
    var pCity = document.createElement("p");
    pCity.textContent = city;
    document.getElementById("title").appendChild(pCity);
    var temp = data.main.temp;
    var pTemp = document.createElement("p");
    pTemp.textContent = temp + "°C";
    document.getElementById("temp").appendChild(pTemp);
    var icon = data.weather[0].icon;
    document.getElementById("icon").src = "http://openweathermap.org/img/w/" + icon + ".png";
    var caption = data.weather[0].description;
    var pCaption = document.createElement("p");
    pCaption.textContent = caption;
    document.getElementById("sky").appendChild(pCaption);
    }
    };




    document.getElementById("fetch").addEventListener('click', function () {
    var city = document.getElementById("city").value;
    if (city == "") {
    alert("Veuillez entrer une ville ! GRINGO !!")
    return;
    } else {
    var apiXhr = new XMLHttpRequest();

    if (apiXhr) {
    apiXhr.onreadystatechange = function () {
    if (apiXhr.readyState === XMLHttpRequest.DONE) {
    if (apiXhr.status === 200) {
    var data = JSON.parse(apiXhr.responseText);
    displayWeather(data, city);

    } else {
    alert('Une erreur s’est produite.');
    }
    }
    };
    apiXhr.open('GET', 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&lang=fr&units=metric&APPID=1bb8b9643fcf40f10d845bc78c254b76');
    apiXhr.send();
    };
    }
    })

    document.getElementById("reset").addEventListener('click', function () {

    var display = document.getElementsByClassName("display");
    if (document.getElementById("city").value == "") {
    alert("Tu n'as pas consulté la météo, GRINGO...");
    return;
    } else {
    document.getElementById("city").value = "";
    document.getElementById("icon").src = "";
    for (let i = 0; i < display.length; i++) {
    var child = display[i].lastElementChild;
    display[i].removeChild(child);
    }
    }
    })
    })