Tuesday, March 27, 2012

Javascript query Yahoo! Weather by zip

Javascript query Yahoo! Weather by zip We can use Javascrip get XML from "http://query.yahooapis.com/v1/public/yql/jonathan/weather?zip=94025", to query Yahoo! Weather by zip code. It's a example to query weather for zip code 94025, Menlo Park, CA.
Javascript query Yahoo! Weather by zip

<html>
<head>
<meta charset="UTF-8">
<title>Mobile-Web-App: Yahoo! Weather by zip</title>

<script>

function loadyahooweather()
{
 if (window.XMLHttpRequest)
 { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 } else {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 
 xmlhttp.open("GET","http://query.yahooapis.com/v1/public/yql/jonathan/weather?zip=94025",false);
 xmlhttp.send();
 xmlDoc=xmlhttp.responseXML; 
 
 //description=xmlDoc.getElementsByTagName("description");
 
 
 description0=(xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue);
 description1=(xmlDoc.getElementsByTagName("description")[1].childNodes[0].nodeValue);
 
 txt = "<p><b>" + description0 + "</b></p>";
 txt += "<p>" + description1 + "</p>";

 //txt = "<p>title: " + "</p>";
   
 document.getElementById("yahooweather").innerHTML=txt;
}
</script>
</head>
<body onload="loadyahooweather()">
<h1>Mobile-Web-App: Yahoo! Weather by zip</h1>
<div id='yahooweather'></div>

</body>
</html>

No comments:

Post a Comment