Google API Loader setOnLoadCallback() - DOM 准备好了吗?
我正在使用 SWFObject,对于替代内容(无 Flash),我想使用 jQuery 插件。
显然我只想在 Flash 不可用时加载 jQuery 和插件脚本。所以 Google 的 API Loader 看起来很完美。
现在我遇到了 setOnLoadCallback()
事件的问题。它似乎应该被触发,但也许是在 DOM 准备好之前? 我发现另一个问题表明有一个第二个未记录的参数,在 DOM 加载时..
但我仍然无法访问 jQuery!
<script type="text/javascript" src="https://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
<script type="text/javascript">
google.load("swfobject", "2.2");
</script>
<script type="text/javascript">
swfobject.embedSWF("slideshow.swf", "slideshow", "800", "530", "7","expressInstall.swf", null, null, null, flashNotLoaded);
function flashNotLoaded(e) {
if (!e.success) {
google.load("jquery", "1.4.2");
google.setOnLoadCallback(jQueryLoaded, true);
}
}
function jQueryLoaded() {
alert("jquery loaded");
$("body").css("background-color","ff00ff"); // does not work....
$(function() {
$("body").css("background-color","ff0000"); // neither does this...
});
}
</script>
编辑:似乎像 jQuery 这样的库的 google.load 只能在 window.load 上使用
只有少数 Google 自有的 API 可以通过回调动态加载
请参阅:Google Library API - google.load 不从事件加载?
I am using SWFObject and for the alternative content (no Flash) I want to use a jQuery plugin.
Obviously I want to load jQuery and the plugin script only when Flash is not available. So Google's API Loader seems perfect.
Now I am having issues with the setOnLoadCallback()
event. It seems to be triggered like it should, but maybe before the DOM is ready?
I found another question on SO revealing there is a second undocumented parameter, on DOM load..
but I still can't access jQuery!
<script type="text/javascript" src="https://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
<script type="text/javascript">
google.load("swfobject", "2.2");
</script>
<script type="text/javascript">
swfobject.embedSWF("slideshow.swf", "slideshow", "800", "530", "7","expressInstall.swf", null, null, null, flashNotLoaded);
function flashNotLoaded(e) {
if (!e.success) {
google.load("jquery", "1.4.2");
google.setOnLoadCallback(jQueryLoaded, true);
}
}
function jQueryLoaded() {
alert("jquery loaded");
$("body").css("background-color","ff00ff"); // does not work....
$(function() {
$("body").css("background-color","ff0000"); // neither does this...
});
}
</script>
EDIT: It seems that google.load for libraries like jQuery is only available on window.load
Only a few of Google own API's can be dynamically loaded with callbacks
See: Google Library API - google.load does not load from event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑当调用 jQueryLoaded 时 DOM 实际上还没有准备好。您应该确保从使用
swfobject.addDomLoadEvent
注册的回调中调用swfobject.embedSWF
。I suspect the DOM isn't actually ready when
jQueryLoaded
is called. You should probably make sureswfobject.embedSWF
is called from a callback registered withswfobject.addDomLoadEvent
.@MPD 注意到 DOM 尚未准备好,因此我使用了
swfobject.addDomLoadEvent
和回调。在这里使用
google.load("jquery", "1.4.3");
jQuery 似乎没有加载?@MPD noticed the DOM is not ready, so I used
swfobject.addDomLoadEvent
with a callback.In here with
google.load("jquery", "1.4.3");
jQuery doesn't seem to load?