2

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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for my late reply. Your suggestion was spot on -- I had entirely forgotten to reconfigure the "setOnLoadCallback" property. You also inspired me to reorganize my code in a more efficient manner. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.