I have a feature on my site where the background changes based on the weather. I can successfully change the background if I declare a static zip code, but I want to be able to automatically fetch the zip code from the user's IP credentials.
I tried incorporating a script to fetch the zip code, which works well, but I can't figure out how to get the zip code into the weather function. I've tried declaring a global variable, but for some reason it doesn't work. I get the error in the console:
Uncaught TypeError: Cannot read property 'text' of undefined
.
Any thoughts on where I went wrong? Thanks!
CODE
var zipcode;
$.getJSON("http://api.ipinfodb.com/v3/ip-city/?key=my_api_key&format=json&callback=?",
function(data){
zipcode = data['zipCode']; //DE,AT ...
});
$(document).ready(function() {
$.YQL = function(query, callback) {
$.getJSON('http://query.yahooapis.com/v1/public/yql?callback=?', { q: query, format: 'json' }, callback);
};
$.YQL("select * from rss where url='http://weather.yahooapis.com/forecastrss?p=" + zipcode + "'", function (data) {
var w = data.query.results.item,
klass = w.condition.text,
current
encodedclass = klass.replace(/\s+/g, '-').toLowerCase();
$('body').addClass(encodedclass);
});
});