Skip to content

Instantly share code, notes, and snippets.

@epk
Created November 18, 2017 04:04
Show Gist options
  • Save epk/1784b61a1d7420dc1c7d39dd922282d1 to your computer and use it in GitHub Desktop.
Save epk/1784b61a1d7420dc1c7d39dd922282d1 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<script>
var xhr;
window.onload = function() {
document.getElementById('getMoreData').addEventListener('click', function(){
// getMyLocation();
jsonp();
clearPage();
});
}
function clearPage(){
var toDelete = document.getElementById("Display");
while (toDelete.hasChildNodes()) {
toDelete.removeChild(toDelete.lastChild);
}
}
function getMyLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocation);
} else {
alert("Oops, no geolocation support");
}
}
function displayLocation(position) {
var lat =parseFloat(position.coords.latitude);
var lon = parseFloat(position.coords.longitude);
}
function jsonp() {
var script = document.createElement('script');
// script.src = 'http://courses.acs.uwinnipeg.ca/2909-050/demos/Week07/XHR/Ex04/data.php?callback=processCallback';
script.src = 'http://courses.acs.uwinnipeg.ca/2909-050/weather.php?callback=processCallback&lat=49.9114745&lon=-97.19827959999999';
document.body.appendChild(script);
}
function processCallback(data){
var ba = JSON.stringify(data);
var bab = JSON.parse(ba);
var temperature = bab.main.temp;
var condition = bab.weather[0].description;
var Desc ="";
if (temperature < 0){
Desc="Its freezing out here!";
}
else if(temperature>0 && temperature <20){
Desc="Sweater Weather";
}
else if(temperature>20){
Desc="Ahh, Summer is here";
}
var div = document.getElementById("Display");
var putDesc = document.createElement("p");
putDesc.innerHTML=Desc;
var putCondition = document.createElement("p");
putCondition.innerHTML=condition;
var putTemp = document.createElement("p");
putTemp.innerHTML="It is currently "+temperature+ " Degrees";
div.appendChild(putDesc);
div.appendChild(putCondition);
div.appendChild(putTemp);
}
</script>
</head>
<body>
<button id="getMoreData">Get Weather</button>
<div id="Display">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment