views:

62

answers:

3

I'm using the Google AJAX Feed API to search for a RSS feed. It works when I just run the javascript in the head of my HTML document in EXAMPLE 1 below (I know I'm getting no result, but that's a problem for another day!), but I need to actually run it when everything's loaded. I'm using jquery, so I've got a $(window).load(function(){}); method, but it doesn't work when I run the Google code in there (EXAMPLE 2). Can anyone see what I'm doing wrong?

EXAMPLE 1 - index.html

<head>
<script type="text/javascript">
console.debug("before google load code");
google.load("feeds", "1");
console.debug("after google load code");
google.setOnLoadCallback(loaded);
function loaded() {
    console.debug("google loaded");
    google.feeds.findFeeds("news", feedSearchDone);
}
function feedSearchDone(result) {
    if (result.error || result.entries.length <= 0) {
              console.debug("No Results Found");
              return;
            }
    else {
         console.debug(result.entries[0].url);
} }
</script>
</head>

EXAMPLE 1 - code.js

$(window).load(function() {
    console.debug("in jquery load function");
});

EXAMPLE 1 - output in Firebug:

before google load code
after google load code
in jquery load function
google loaded
No Results Found

EXAMPLE 2 - code.js

$(window).load(function() {

    console.debug("in jquery load function");

    console.debug("before google load code");
    google.load("feeds", "1");
    console.debug("after google load code");
    google.setOnLoadCallback(loaded);

    function loaded() {
        console.debug("google loaded");
        google.feeds.findFeeds("news", feedSearchDone);
    }

    function feedSearchDone(result) {
        if (result.error || result.entries.length <= 0) {
                  console.debug("No Results Found");
                  return;
                }
        else {
        console.debug(result.entries[0].url);

    } } 
});

EXAMPLE 2 - output in Firebug

in jquery load function
before google load code
window.loadFirebugConsole is not a function
[Break on this error] <html lang="en">

Even though the error refers to Firebug, I don't think that's the cause because it the page behaves the same (no elements are on the screen) when I view it in Safari. Thanks for reading.

A: 

Are you definitely including jquery above your $ doc.ready?

Haroldo
the "in jquery load function" line in "EXAMPLE 2 - output in Firebug" indicates to me that jquery is working okay? I'm not very experienced with jquery though, I may be wrong?
ben
+1  A: 

You have to use $(document).ready instead, the scripts will only load after the DOMLoad completes.

BrunoLM
I tried this but I'm getting the same error. I'm not sure this is the problem, the load function is running because the "in jquery load function" line in "EXAMPLE 2 - output in Firebug" comes from there
ben
Are you loading this script dynamically? If yes you need to wait it to load. Here is a example http://stackoverflow.com/questions/3084371/lazy-loading-a-plugin-jquery/3084435#3084435 - the weird this is that you are already using google.setOnCallBack funcion and it's not working, check my link that might do the trick.
BrunoLM
+1  A: 

The JQuery onload can ONLY be used after the google onLoad callback.

function initialize() {
    $(function () {
        // Some other code here
    });
}
google.setOnLoadCallback(initialize);

Try adding that to the code.js file.

Xenph Yan
Thanks for this. I'm new to jquery, how do I accept the result object in the initialize function?
ben
Sorry don't worry, my question makes no sense.
ben