[英]google.setOnLoadCallback with jQuery $(document).ready(), is it OK to mix?
I'm using Google Ajax API and they suggest I use google.setOnLoadCallback()
to do various things related to their API but I'm using also jQuery's $(document).ready()
to do other JS things, not related to Google API. 我正在使用Google Ajax API,他们建议我使用google.setOnLoadCallback()
来处理与其API相关的各种事情,但我也使用jQuery的$(document).ready()
来做其他与Google无关的JS事情API。
Is it safe to mix these two approaches in one document? 将这两种方法混合在一个文档中是否安全? I did not notice any problems yet but I suppose it's a matter of scale. 我没有注意到任何问题,但我认为这是一个规模问题。
You pretty much have to do this: 你几乎必须这样做:
google.setOnLoadCallback(function() {
$(function() {
// init my stuff
});
});
You can't do $(document).ready()
without $
(the jQuery object) being available, so that needs to go inside the callback. 你不能在没有$
(jQuery对象)的情况下执行$(document).ready()
,因此需要进入回调。 And you can't be sure the document is ready inside the callback, so you have to do ready()
too. 而且您无法确定文档在回调中是否已ready()
,因此您也必须做好ready()
。
Sorry to be raising this from the dead, but 1) It still comes up as an 'answer' to this problem and 2) I've found a better solution. 很抱歉从死里复活,但1)它仍然是这个问题的“答案”,2)我找到了一个更好的解决方案。
There is an optional 3rd argument on the google.load
function that takes an object of configuration options. google.load
函数中有一个可选的第3个参数,它接受配置选项的对象。 One of the options is callback
. 其中一个选择是callback
。 It also gets rid of the need for a separate setOnLoadCallback
call. 它还摆脱了单独的setOnLoadCallback
调用的需要。
Eg 例如
google.load('visualization', '1.0', {
'packages': "charttype",
'callback': $jQ.proxy(me.setupChart, me)
});
So: 所以:
<script src="https://www.google.com/jsapi"></script>
<script>
$(document).ready(function () {
function mapsLoaded() {
etc etc etc
}
google.load("maps", "2", {"callback" : mapsLoaded});
});
</script>
See: https://developers.google.com/loader/#Dynamic 请参阅: https : //developers.google.com/loader/#Dynamic
If your JavaScript code resides in its own js file and not inside the HTML document you could also do this in the document: 如果您的JavaScript代码驻留在自己的js文件中而不是HTML文档中,您也可以在文档中执行此操作:
<script>
google.load("jquery", "1.7.0");
google.load("jqueryui", "1.8.16");
google.setOnLoadCallback(function() {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "my.js");
document.getElementsByTagName("html")[0].appendChild(script);
});
</script>
This loads my.js
after all other stuff is loaded from google. 在从谷歌加载所有其他内容后加载my.js
In your my.js
file you can then do $(document).ready(...)
. 在my.js
文件中,您可以执行$(document).ready(...)
。 So your application code is independent from "loaded by google" or "loaded directly from your server". 因此,您的应用程序代码独立于“由Google加载”或“直接从您的服务器加载”。
Why mix when you can do it all with $(document).ready()
? 为什么要混合使用$(document).ready()
? Just get rid of the google.setOnLoadCallback
function and use jQuery's $(document).ready()
. 只需摆脱google.setOnLoadCallback
函数并使用jQuery的$(document).ready()
。
This: 这个:
google.setOnLoadCallback(chartEnrollment);
becomes 变
$(document).ready(chartEnrollment);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.