简体   繁体   English

调用 function onload 外部脚本

[英]call a function onload of external script

I'm playing around with Google Drive API, and noticed that they are calling a handleClientLoad function onload of the client.js.我正在玩 Google Drive API,并注意到他们正在调用 client.js 的handleClientLoad function onload

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Trying to avoid creating globals, I thought I would start with creating another js file, that would contain a module pattern and return handleClientLoad .为了避免创建全局变量,我想我会从创建另一个 js 文件开始,该文件将包含一个模块模式并返回handleClientLoad

var module = (function (window, $) {

    'use strict';

     var module = {
         handleClientLoad: function () {
             console.log('ok, can access');
         }
     };

     return module;

}(window, jQuery));

And then I assumed I could just call the handleClientLoad by doing module.handleClientLoad , but that doesn't seem to be working.然后我假设我可以通过执行module.handleClientLoad来调用handleClientLoad ,但这似乎不起作用。

    <script src="scripts/main.js"></script>
    <script src="https://apis.google.com/js/client.js?onload=module.handleClientLoad"></script>

Questions:问题:

  1. Is it possible to call the module.handleClientLoad from onload of client.js?是否可以从 client.js 的 onload 调用module.handleClientLoad

  2. Appending onload and calling a function from a script file seems sloppy and obtrusive, no?追加onload并从脚本文件调用 function 似乎草率和突兀,不是吗? Is there a cleaner way to know when the client.js has loaded?有没有更清晰的方法来了解 client.js 何时加载?

  1. Have you tried debugger, and are you sure module.你有没有试过调试器,你确定 module. hanfleClientLoad exists at the time the callback is fired?触发回调时是否存在 hanfleClientLoad?

  2. You can poll for the existence of gapi.client as a global object. Give it a few milliseconds to initialise before invoking its methods.您可以轮询是否存在 gapi.client 作为全局 object。在调用其方法之前给它几毫秒的初始化时间。

I found that jQuery.getScript() worked quite nicely for this.我发现jQuery.getScript()对此非常有效。 Documentation here .文档在这里

Instead including the <script> tag in the html page, I simply included the following line in my js file:我没有在 html 页面中包含<script>标记,而是在我的 js 文件中包含了以下行:

jQuery.getScript( "https://apis.google.com/js/api.js", handleClientLoad );

It might require slight tweaking for the way you structured your module, but I think this will be easier than passing a parameter to your <script> tag (at least I found it easier).它可能需要对您构建模块的方式进行轻微调整,但我认为这比将参数传递给<script>标记更容易(至少我发现它更容易)。

I know this is old, but I was messing around with this because this was related to a question on a test I was studying for.我知道这很旧,但我一直在搞这个,因为这与我正在学习的考试中的一个问题有关。 You can use onload like this when you call the script:调用脚本时可以像这样使用 onload:

<script src="https://apis.google.com/js/client.js" onload="handleClientLoad()"></script>

For anyone wanting to know why this won't work:对于任何想知道为什么这行不通的人:

<script src="https://apis.google.com/js/client.js">handleClientLoad()</script>

It's because any code between a script tag with "src=" in it will be ignored.这是因为带有“src=”的脚本标签之间的任何代码都将被忽略。

And I'm not sure why using onload= in the script tag calling the external script is any more obtuse than appending?onload=module.handleClientLoad to the source?而且我不确定为什么在调用外部脚本的脚本标记中使用 onload= 比将?onload=module.handleClientLoad 附加到源代码更迟钝? But that's just me.但那只是我。

In the end, I'm not sure why exactly this was a question on the test, because based on searching, this doesn't seem to be a common thing that anyone does.最后,我不确定为什么这是测试题,因为根据搜索,这似乎不是任何人都会做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM