最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Deciphering the Google Search API: how to refer to an argument passed into callback function? - Stack Overflow

programmeradmin0浏览0评论

I'm using the Google Search API to load some Search results into my page. I'd like to set an argument to the callback function that says which div the search results should attach themselves to.

Here's the callback function definition, per Google:

.setSearchCompleteCallback(object, method, opt_arguments?)

Here's how I'm doing it: searcher.setSearchCompleteCallback (document, function() { alert(opt_arguments[0].id); }, new Array(infodiv) );

The documentation explains: "Applications may optionally pass in a context argument using opt_arguments which is then passed to the specified method."

Yes -- but how? I've passed in the context argument, but how do I refer to it within the function? I've tried just calling opt_arguments, but js errors clearly show that it's not defined.

The documentation is here.

Thank you!!

I'm using the Google Search API to load some Search results into my page. I'd like to set an argument to the callback function that says which div the search results should attach themselves to.

Here's the callback function definition, per Google:

.setSearchCompleteCallback(object, method, opt_arguments?)

Here's how I'm doing it: searcher.setSearchCompleteCallback (document, function() { alert(opt_arguments[0].id); }, new Array(infodiv) );

The documentation explains: "Applications may optionally pass in a context argument using opt_arguments which is then passed to the specified method."

Yes -- but how? I've passed in the context argument, but how do I refer to it within the function? I've tried just calling opt_arguments, but js errors clearly show that it's not defined.

The documentation is here.

Thank you!!

Share Improve this question edited Feb 3, 2010 at 21:36 Summer asked Feb 3, 2010 at 21:29 SummerSummer 2,4983 gold badges23 silver badges32 bronze badges 1
  • can you post some sample code so that we can see how you tried to do it? – ithcy Commented Feb 3, 2010 at 21:32
Add a ment  | 

3 Answers 3

Reset to default 6

Basically, it means the following. You can bind the event handler like such:

function searchComplete(message) {
   alert(message);
}

function OnLoad() {
  var searchControl = new google.search.SearchControl();
  var webSearch = new google.search.WebSearch();

  searchControl.addSearcher(webSearch);
  searchControl.draw(document.getElementById("searchcontrol"));
  searchControl.setSearchCompleteCallback(this, searchComplete, "Search Done!");

  searchControl.execute('Google')
}
google.setOnLoadCallback(OnLoad);

The example code above will display a message saying "Search Done!" when finished.

It should look something like this:

var myCallbackObject = 
{
    myCallbackFunction: function(args)
    {
        // args will be whatever someArgs is set to below
        alert(args); // Array("hey","hello")
    }
}

var someArgs = ["hey", "hello"];
// (... set up mySearchObject as the google Search object here)
mySearchObject.setSearchCompleteCallback(myCallbackObject, myCallbackFunction, someArgs);

If you pass a context argument, your callback method should take that context argument as a parameter.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论