Javascript google.setOnLoadCallback(initialize) 函数究竟是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2573549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:58:39  来源:igfitidea点击:

What does google.setOnLoadCallback(initialize) function exactly mean?

javascriptsearch-enginegoogle-api

提问by Abhilash Muthuraj

While coding JavaScript and Ajax, there is no proper documentation for this function. I searched this term using api src="http://www.google.com/jsapi"and searchControl.execute("abhilashm86");.

在编写 JavaScript 和 Ajax 时,没有针对此功能的适当文档。我使用api src="http://www.google.com/jsapi"and搜索了这个词searchControl.execute("abhilashm86");

How is this google.setOnLoadCallback(initialize)called internally? Is this function just for a new search term when the user clears previous search and starts a new one? How exactly does google.setOnLoadCallback(initialize)get trigerred?

这在google.setOnLoadCallback(initialize)内部如何调用?当用户清除以前的搜索并开始新的搜索时,此功能是否仅适用于新的搜索词?究竟是如何google.setOnLoadCallback(initialize)触发的?

回答by Matthew Flaschen

Your initialize function will be called when your document (including the Google API) is fully loaded. So you should wrap any code that uses the API in the function you pass in:

当您的文档(包括 Google API)完全加载时,您的 initialize 函数将被调用。因此,您应该将任何使用 API 的代码包装在您传入的函数中:

E.g.

例如

google.setOnLoadCallback(function()
{
   var searcher = new google.search.SearchControl();
   // More code that depends on the API.
});

回答by Jason Hall

The documentationsays:

文档说:

Use google.setOnLoadCallback to register the specified handler function to be called once the document loads instead of GSearch.setOnLoadCallback.

使用 google.setOnLoadCallback 注册指定的处理程序函数以在文档加载后调用,而不是 GSearch.setOnLoadCallback。