有什么方法可以在 window.onload 中设置 google onload 回调吗?
这是我的代码:
window.onload = function(){
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
alert("Message");
});
}
有什么办法让这个代码工作吗?
here is my code:
window.onload = function(){
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
alert("Message");
});
}
Is there any way to let this code work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自谷歌文档的函数:
它似乎有回调对象,你可以在这里分配你喜欢的任何函数。
谷歌说:
Function from Google Documentation:
It seems it has callback object and you can assign here any function you like.
Google says:
我怀疑您遇到的问题是在使用
google.load
的代码之前使用它。如果我没记错的话,该函数将一堆脚本标签注入到 DOM 中,以便检索各种 JavaScript 依赖项。如果您将使用这些依赖项的代码放在与加载相同的脚本标记中,则依赖项将尚未被处理。尝试:看看上面的方法是否有效。您还可以尝试添加一些日志记录,以查看调用了什么(如果有)以及何时调用各个代码位(例如,您是否将 window.onload 设置得太晚了?)。
I suspect the problem you are having is the use of
google.load
right before the code that makes use of it. If I recall correctly, that function injects a bunch of script tags into the DOM in order to retrieve the various JavaScript dependencies. If you put the code that makes use of these dependencies in the same script tag as the load, then the dependency won't have been processed yet. Try:And see if the above works. You might also try adding some logging to see what, if anything, gets invoked as well as when the various bits of code get invoked (e.g. did you set window.onload too late?).