|
-
Feb 3rd, 2012, 03:56 PM
#1
xml_request.onload
ok, i have this code (there is a lot more to it)
that i found in a theme (for iPhone) - it fetches weather from Yahoo
Code:
function fetchWeatherData (callback, zip)
{
url="http://weather.yahooapis.com/forecastrss?u=f&w=" //u=Farenheit, because accuWeather sucks
var xml_request = new XMLHttpRequest();
xml_request.onload = function(e) {xml_loaded(e, xml_request, callback);}
xml_request.overrideMimeType("text/xml");
xml_request.open("GET", url+zip);
xml_request.setRequestHeader("Cache-Control", "no-cache");
xml_request.send(null);
return xml_request;
}
function xml_loaded (event, request, callback)
{
if (request.responseXML)
{
var obj = {error:false, errorString:null};
var effectiveRoot = findChild(findChild(request.responseXML, "rss"), "channel");
obj.city = findChild(effectiveRoot, "yweather:location").getAttribute("city");
obj.realFeel = findChild(effectiveRoot, "yweather:wind").getAttribute("chill");//Only accounts for windChill
obj.sunrise = findChild(effectiveRoot, "yweather:astronomy").getAttribute("sunrise");
obj.sunset = findChild(effectiveRoot, "yweather:astronomy").getAttribute("sunset");
conditionTag = findChild(findChild(effectiveRoot, "item"), "yweather:condition");
obj.temp = conditionTag.getAttribute("temp");
obj.icon = conditionTag.getAttribute("code");
obj.description = conditionTag.getAttribute("text");
callback (obj);
}else{
callback ({error:true, errorString:"XML request failed. no responseXML"});
}
}
the xml_request.onload = function(e) etc etc.. never seems to trigger?
ive put alerts before and after - and they go.. but if i put an alert in the xml_loaded function, it never triggers.
Now i know this works - because of the theme i am using it from. its just the theme is coded so poorly that i am trying to pull it apart and put it back together to make better use of it.
i read in one place that the function(e) is only for webkit... but when i tested it on my iphone, it doesnt work.
any ideas? or... a better way to grab/read weather?
Thanks!
(here is the entire weather.js)
Code:
var locale = "12763236"
var isCelsius = false
var updateInterval = 5
var postal;
var demoMode = false;
var enabled;
function onLoad(){
validateWeatherLocation(escape(locale).replace(/^%u/g, "%"), setPostal)
}
function convertTemp(num)
{
if (isCelsius == true)
return Math.round ((num - 32) * 5 / 9);
else
return num;
}
function setPostal(obj){
if (obj.error == false){
if(obj.cities.length > 0){
postal = escape(obj.cities[0].zip).replace(/^%u/g, "%");
//document.getElementById("WeatherContainer").className = "";
weatherRefresherTemp();
}else{
document.getElementById("city").innerText="Not Found";
//document.getElementById("WeatherContainer").className = "errorLocaleNotFound";
}
}else{
document.getElementById("city").innerText=obj.errorString;
//document.getElementById("WeatherContainer").className = "errorLocaleValidate";
setTimeout('validateWeatherLocation(escape(locale).replace(/^%u/g, "%"), setPostal)', Math.round(1000*60*5));
}
}
function dealWithWeather(obj){
if (obj.error == false){
if(useRealFeel == true){
tempValue = convertTemp(obj.realFeel);
}else{
tempValue = convertTemp(obj.temp);
}
document.getElementById("city").innerHTML="Location: " + obj.city.toUpperCase();
document.getElementById("temp").innerHTML=obj.description.toUpperCase();
document.getElementById("cond").innerHTML="Ambient Temp: " + tempValue + "°"
}else{
document.getElementById("city").innerHTML = "errorWeatherDataFetch";
}
}
function weatherRefresherTemp(){
fetchWeatherData(dealWithWeather,postal);
}
var MiniIcons =
[
"tornado", //0 tornado X
"tropical_storm", //1 tropical storm X
"hurricane", //2 hurricane X
"severe_thunderstorms", //3 severe thunderstorms X
"thunderstorms", //4 thunderstorms X
"rain_snow", //5 mixed rain and snow X
"rain_sleet", //6 mixed rain and sleet X
"snow_sleet", //7 mixed snow and sleet X
"freezing_drizzle", //8 freezing drizzle X
"light_rain", //9 drizzle X
"freezing_rain", //10 freezing rain X
"light_rain", //11 showers X
"showers", //12 showers X
"snow_flurries", //13 snow flurries X
"light_snow", //14 light snow showers X
"blowing_snow", //15 blowing snow X
"snow", //16 snow X
"hail", //17 hail X
"sleet", //18 sleet X
"dust", //19 dust X
"fog", //20 foggy X
"haze", //21 haze X
"smoky", //22 smoky X
"blustery", //23 blustery X
"windy", //24 windy X
"overcast", //25 cold X
"cloudy", //26 cloudy X
"mostly_cloudy_night", //27 mostly cloudy (night) X
"mostly_cloudy", //28 mostly cloudy (day) X
"partly_cloudy_night", //29 partly cloudy (night) X
"partly_cloudy", //30 partly cloudy (day) X
"clear", //31 clear (night) X
"clear", //32 sunny X
"fair_night", //33 fair (night) X
"fair", //34 fair (day) X
"rain_hail", //35 mixed rain and hail X
"hot", //36 hot X
"iso_thnderstrm", //37 isolated thunderstorms X
"sctrd_thnderstrm", //38 scattered thunderstorms X
"sctrd_thnderstrm", //39 scattered thunderstorms X
"sctrd_showers", //40 scattered showers X
"heavy_snow", //41 heavy snow X
"snow3", //42 scattered snow showers
"heavy_snow", //43 heavy snow X
"partly_cloudy", //44 partly cloudy X
"thundershowers", //45 thundershowers X
"light_snow", //46 snow showers X
"iso_thundershowers", //47 isolated thundershowers X
"dunno", //3200 not available X
]
function constructError (string)
{
return {error:true, errorString:string};
}
function findChild (element, nodeName)
{
var child;
for (child = element.firstChild; child != null; child = child.nextSibling)
{
if (child.nodeName == nodeName)
return child;
}
return null;
}
function fetchWeatherData (callback, zip)
{
url="http://weather.yahooapis.com/forecastrss?u=f&w=" //u=Farenheit, because accuWeather sucks
var xml_request = new XMLHttpRequest();
xml_request.onload = function(e) {xml_loaded(e, xml_request, callback);}
xml_request.overrideMimeType("text/xml");
xml_request.open("GET", url+zip);
xml_request.setRequestHeader("Cache-Control", "no-cache");
xml_request.send(null);
return xml_request;
}
function xml_loaded (event, request, callback){
if (request.responseXML)
{
var obj = {error:false, errorString:null};
var effectiveRoot = findChild(findChild(request.responseXML, "rss"), "channel");
obj.city = findChild(effectiveRoot, "yweather:location").getAttribute("city");
obj.realFeel = findChild(effectiveRoot, "yweather:wind").getAttribute("chill");
obj.sunrise = findChild(effectiveRoot, "yweather:astronomy").getAttribute("sunrise");
obj.sunset = findChild(effectiveRoot, "yweather:astronomy").getAttribute("sunset");
conditionTag = findChild(findChild(effectiveRoot, "item"), "yweather:condition");
obj.temp = conditionTag.getAttribute("temp");
obj.icon = conditionTag.getAttribute("code");
obj.description = conditionTag.getAttribute("text");
callback (obj);
}else{
callback ({error:true, errorString:"XML request failed. no responseXML"});
}
}
function validateWeatherLocation (location, callback)
{
var obj = {error:false, errorString:null, cities: new Array};
obj.cities[0] = {zip: location}; //Not very clever, are we?
callback (obj);
}
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|