Ajax & Google & JavaScriptSunday, October 19th, 2008

NewsBar - No Results -

Few days ago one of the readers of my blog ask me how to get - No Results - in the Google NewsBar. Those who play with NewsBar knows that when query gives you no results the applications switch to default query (which is “Google”) and shows you news about the Google.
How to bypass the default query? Simple, just put the following line before you create newsBar object.

GSnewsBar.DEFAULT_QUERY = "";

Example from the Google NewsBar documentation:

function LoadNewsBar() {
var root = document.getElementById(”newsBarTop”);
var options = {
largeResultSet : false,
resultStyle : GSnewsBar.RESULT_STYLE_EXPANDED,
title : “Nintendo in the news”,
autoExecuteList : {
executeList : [ "Nintendo", "Nintendo DS", "Nintendo Wii" ]
}
};
GSnewsBar.DEFAULT_QUERY = ""; // bypassing the default query!
var newsBar = new GSnewsBar(root, options);
}

You can specify the default query, when queries from executeList produce an empty list of results.

Also, you probably want to count the number of result? It’s not that simple, because you will have to modify the function in gnewsbar.js and we don’t want to do that. The problem is that you have to wait until all data are loaded and then check the number of results.

Here is the solution, not very good, but if you find better one please post comment here.

[...]

GSnewsBar.DEFAULT_QUERY = ""; // bypassing the default query!
var newsBar = new GSnewsBar(root, options);

window.setTimeout(function(){
if (newsBar.ns.results.length==0)  document.getElementById(”<id of the newsbar container>”).innerHTML=’No Results’;
},2000);

[...]

This function waits 2sec (2000ms) and then check if results are available, if not then it shows the ‘No Results’ text.




Leave a Reply

  • Name: (required)
  • E-mail: (not required/will not be published)
  • Website:
  • Comment: (required)
  • Enter Above Code:
  • Letters left: 255
  • Note: Your comment will appear on the site within a few hours after you submit it.