我正在使用 Google AJAX Feed API 来搜索 RSS 提要。当我在下面的示例 1 中运行我的 HTML 文档头部的 javascript 时它起作用了(我知道我没有得到结果,但那是另一天的问题!),但我需要在加载所有内容时实际运行它。我正在使用 jquery,所以我有一个 $(window).load(function(){});方法,但是当我在那里运行谷歌代码时它不起作用(示例 2)。谁能看出我做错了什么?
示例 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>
示例 1 - code.js
$(window).load(function() {
console.debug("in jquery load function");
});
示例 1 - Firebug 中的输出:
before google load code
after google load code
in jquery load function
google loaded
No Results Found
示例 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);
} }
});
示例 2 - Firebug 中的输出
in jquery load function
before google load code
window.loadFirebugConsole is not a function
[Break on this error] <html lang="en">
尽管错误是指 Firebug,但我认为这不是原因,因为当我在 Safari 中查看该页面时,该页面的行为相同(屏幕上没有任何元素)。感谢阅读。
最佳答案
JQuery onload 只能在 google onLoad 回调之后使用。
function initialize() {
$(function () {
// Some other code here
});
}
google.setOnLoadCallback(initialize);
尝试将其添加到 code.js 文件中。
关于javascript - google.load 在 jquery 的 $(window).load(function(){}) 中运行时不起作用;?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082482/