google.setOnLoadCallback 与 jQuery $(document).ready() 可以混合使用吗?

发布于 2024-07-13 22:37:59 字数 202 浏览 7 评论 0原文

我正在使用 Google Ajax API,他们建议我使用 google.setOnLoadCallback() 来执行与其 API 相关的各种操作,但我也使用 jQuery 的 $(document).ready() 做其他 JS 事情,与 Google API 无关。

将这两种方法混合在一个文档中是否安全? 我还没有注意到任何问题,但我认为这是一个规模问题。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蝶…霜飞 2024-07-20 22:37:59

你几乎必须这样做:

google.setOnLoadCallback(function() {
  $(function() {
    // init my stuff
  });
});

如果没有可用的 $ (jQuery 对象),你就无法执行 $(document).ready() ,所以需要这样做在回调里面。 并且您无法确定文档在回调中是否已准备好,因此您也必须执行ready()。

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. And you can't be sure the document is ready inside the callback, so you have to do ready() too.

淡淡绿茶香 2024-07-20 22:37:59

很抱歉让这个问题复活,但是 1)它仍然是这个问题的“答案”,2)我找到了一个更好的解决方案。

google.load 函数上有一个可选的第三个参数,它接受配置选项的对象。 其中一个选项是回调。 它还消除了对单独的 setOnLoadCallback 调用的需要。

例如

google.load('visualization', '1.0', {
    'packages': "charttype", 
    'callback': $jQ.proxy(me.setupChart, me)
});

<script src="https://www.google.com/jsapi"></script>
<script>
$(document).ready(function () {
    function mapsLoaded() {
        etc etc etc
    }

    google.load("maps", "2", {"callback" : mapsLoaded});
});
</script>

参见:
https://developers.google.com/loader/#Dynamic

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.

There is an optional 3rd argument on the google.load function that takes an object of configuration options. One of the options is callback. It also gets rid of the need for a separate setOnLoadCallback call.

E.g.

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

2024-07-20 22:37:59

如果您的 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>

这会在从 google 加载所有其他内容后加载 my.js 。 然后,您可以在 my.js 文件中执行 $(document).ready(...) 操作。 因此,您的应用程序代码独立于“由谷歌加载”或“直接从您的服务器加载”。

If your JavaScript code resides in its own js file and not inside the HTML document you could also do this in the document:

<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. In your my.js file you can then do $(document).ready(...). So your application code is independent from "loaded by google" or "loaded directly from your server".

诗笺 2024-07-20 22:37:59

当您可以使用 $(document).ready() 完成所有操作时,为什么要混合使用呢? 只需摆脱 google.setOnLoadCallback 函数并使用 jQuery 的 $(document).ready() 即可。

这:

google.setOnLoadCallback(chartEnrollment);

变成

$(document).ready(chartEnrollment);

Why mix when you can do it all with $(document).ready()? Just get rid of the google.setOnLoadCallback function and use jQuery's $(document).ready().

This:

google.setOnLoadCallback(chartEnrollment);

becomes

$(document).ready(chartEnrollment);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文