views:

76

answers:

0

I am trying to pas a variable using the Google search API.

To speed things up for you I believe the problem is with the third last line:

imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);

I have a number of div elements, each with their own specific id (ie. "showImagesResults100" and "showImagesResults102", etc). var itemid stores the number portion of each specific id.

I am unable to output the search image results from function searchComplete() into the respective showImagesResultsXXX div because I cannot pass the itemid variable to the searchComplete function using the imageSearch.setSearchcompleteCallback function.

Can someone please tell me how this is done? thanks

    function searchTheGoogleImagesAndDisplayThem(itemid){
       // get google images using the Google search API
       var imageSearchQuery = document.getElementById('theTopicTitle' + itemid);
       searchGoogleImages(imageSearchQuery,itemid);
    }

    function searchComplete(searcher,itemid) {
      // Check that we got results
      if (searcher.results && searcher.results.length > 0) {
        // Grab our content div, clear it.
        //alert('showImagesResults' + itemid);
        var contentDiv = document.getElementById('showImagesResults' + itemid);
        contentDiv.innerHTML = '';

    // Loop through our results, printing them to the page.
    var results = searcher.results;
    for (var i = 0; i < results.length; i++) {
      // For each result write it's title and image to the screen
      var result = results[i];
      var imgContainer = document.createElement('span');

      var title = document.createElement('h2');
      // We use titleNoFormatting so that no HTML tags are left in the title
      title.innerHTML = result.titleNoFormatting;

      var newImg = document.createElement('img');
      // There is also a result.url property which has the escaped version
      newImg.src = result.tbUrl;

      imgContainer.appendChild(title);
      imgContainer.appendChild(newImg);

      // Put our title + image in the content
      contentDiv.appendChild(imgContainer);
    }
  }
}

function searchGoogleImages(imageSearchQuery,itemid) {
  // Our ImageSearch instance.
  var imageSearch = new google.search.ImageSearch();

  // Restrict to extra large images only
  imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
                             google.search.ImageSearch.IMAGESIZE_MEDIUM);

  // Here we set a callback so that anytime a search is executed, it will call
  // the searchComplete function and pass it our ImageSearch searcher.
  // When a search completes, our ImageSearch object is automatically
  // populated with the results.
  imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);  // need to pass itemid to searchComplete function on this line somehow

  // Find me images for the search query.
  imageSearch.execute(imageSearchQuery);
}