Google setOnloadcallback() 函数与 window.onload 冲突
这是我的代码:
window.onload = function(){
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
alert("Message");
});
}
我知道如果我将 google load 放在 window.onload 之外,它会起作用。但我希望它位于 window.onload 内。有什么建议吗?
谢谢
here is my code:
window.onload = function(){
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
alert("Message");
});
}
I know it will work if I put google load outside window.onload. But I want it to be inside the window.onload. Any suggestions?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,Google 加载器正在使用
document.write
将脚本添加到页面。document.write
仅在窗口完全加载之前起作用,因此您似乎无法在window.onload
内部使用google.load
。我建议将您需要的任何代码放入google.setOnLoadCallback
函数中 -window.onload
在那里工作得很好。或者,您可以自动加载 Google API,这样就不需要 onLoadCallback(有关更多信息,请参阅文档):
另请查看 jQuery 库,因为它具有类似
$(document).ready()
的函数大大改善了 window.onload。As far as I know, the Google loader is using
document.write
to add scripts to the page.document.write
only works before the window has fully loaded, so it seems you can't usegoogle.load
inside ofwindow.onload
. I'd recommend putting whatever code you need inside of thegoogle.setOnLoadCallback
function -window.onload
works fine in there.Alternatively, you can autoload the Google APIs so that the onLoadCallback is unnecessary like this (see docs for more info):
Also check out the jQuery library because it has functions like
$(document).ready()
that improve greatly on window.onload.