开发者

Javascript help needed - which variable is return empty?

I would like to know how do I add an error check to below mentioned code...I mean how do I check if this code return empty or not?? if this returns empty then I would give a message "Not Found".. How do I do That??

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 sea开发者_开发百科rch 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);

  // Set a site restriction
  blogSearch.setSiteRestriction('blogspot.com');

  // 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);


You already have a check to see if the the search results aren't empty:

 if (blogSearch.results && blogSearch.results.length > 0) {

All you need to do as add an else block just after the if block's closing bracket:

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);
    }
  }
  else {  // <-- HERE!
      document.getElementById("content").innerHTML = "Not found.";
  }
}


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);
 }
} else {
  document.getElementById('content').innerHTML = 'Not found!'
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜