JGallardo JGallardo - 4 years ago 104
Javascript Question

Javascript and HTML to display temperature and location from Yahoo API


How can I build a minimal working sample on a site like codepen showing a location and it's temperature using the Yahoo weather API. I need specifically San Diego, CA. And using only HTML and Javascript, not PHP.


I did check the site for a similar question but it only addressed temperature Getting only temperature from Yahoo Weather but it's only answer linked to an overcomplicated tutorial with excessive code.

Other answers on the site only have YML but don't show how to integrate an entire working example.

I was following along to the documentation from Yahoo but there is no working example like how NASA has a live example


I have this CodePen demo


<div id="output"></div>


$(document).ready(function () {
$.getJSON("https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%202487889&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys/", function (data) {
$('#output').append( 'The temperature in' + result.location.["location"] + 'is' + result.condition.["temp"] );

Answer Source

Here's a working example based on your original code.

Something to note: you were doing this result.location.["location"] Which is invalid. You could use result.location["location"] or result.location.location (neither of which are returned in your result btw)

var queryURL = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%202487889&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys/";

$.getJSON(queryURL, function (data) {
  var results = data.query.results
  var firstResult = results.channel.item.condition
  var location = 'Unknown' // not returned in response
  var temp = firstResult.temp
  var text = firstResult.text
  $('#output').append('The temperature is ' + temp + '. Forecast calls for '+text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download