返回顶部

收藏

Google 博客搜索的 Hello World

更多

此代码用于在网页上调用 Google 的博客搜索API

// The "Hello, World" of Blog Search

// This code generates a "Raw Searcher" to handle search queries. The Raw Searcher requires
// you to handle and draw the search results manually.
google.load('search', '1');

var blogSearch;

function searchComplete() {
  // Check that we got results
  document.getElementById('content').innerHTML = '';
  if (blogSearch.results && blogSearch.results.length > 0) {
    for (var i = 0; i < blogSearch.results.length; i++) {

      // Create HTML elements for search results
      var p = document.createElement('p');
      var a = document.createElement('a');
      a.href = blogSearch.results[i].postUrl;
      a.innerHTML = blogSearch.results[i].title;

      // Append search results to the HTML nodes
      p.appendChild(a);
      document.body.appendChild(p);
    }
  }
}

function onLoad() {

  // Create a BlogSearch instance.
  blogSearch = new google.search.BlogSearch();

  // Set searchComplete as the callback function when a search is complete.  The
  // blogSearch object will have results in it.
  blogSearch.setSearchCompleteCallback(this, searchComplete, null);

  // Execute search query
  blogSearch.execute('1974 Chevrolet Caprice');

  // Include the required Google branding
  google.search.Search.getBranding('branding');
}

// Set a callback to call your code when the page loads
google.setOnLoadCallback(onLoad);
//该片段来自于http://outofmemory.cn

标签:javascript,网络

收藏

0人收藏

支持

0

反对

0

发表评论