Reputation: 1146
I'm trying to implement a few feeds into my site using a div-populating function. Previously, I had identical JavaScript code blocks through my page (one for each feed), but in the interest of being a good (or better) coder, I decided to stick the repetitive code into an external script file and run it with varying parameters instead.
Unfortunately, the feeds refuse to load with my new implementation, and I'm running out of ideas. I structured my callback function based on user ndp's interesting suggestions in question 9662168.
So, I have the following two functions in my external script file (minor code redaction for simplicity):
function populateRSSFeed(divID) {
var targetDiv = document.getElementById(divID);
return function callback(result) {
if (!result.error) {
var container = document.getElementById(targetDiv);
var list = document.createElement('ul');
<snip: div-population code here>
container.appendChild(list);
}
else
alert('Error fetching feeds!');
}
}
function initializeRSSFeed(callback, targetFeed) {
google.load('feeds','1');
var feed = new google.feeds.Feed(targetFeed);
var numEntries = 3;
feed.setNumEntries(numEntries);
feed.load(callback);
}
These functions are called as follows in my HTML:
<script>
var callback = populateRSSFeed('feed-list-wrapper-1');
initializeRSSFeed(callback, 'http://rss.cnn.com/rss/cnn_topstories.rss');
</script>
The error message I'm receiving makes me think that something is going wrong at the feed-loading stage, but I can't determine the cause.
Uncaught TypeError: Cannot read property 'Feed' of undefined
What do you guys think?
Upvotes: 2
Views: 3701
Reputation: 102763
It looks like you're not using setOnLoadCallback
to wait for the Google scripts to load. That would explain why google.feeds
is undefined (resulting directly in the error you're seeing).
Try using this script instead (I just followed the "Hello World" example in the Developers' Guide).
google.load('feeds','1');
function onGoogleReady() {
var callback = populateRSSFeed('feed-list-wrapper-1');
initializeRSSFeed(callback, 'http://rss.cnn.com/rss/cnn_topstories.rss');
}
google.setOnLoadCallback(onGoogleReady);
Upvotes: 1