The way we’ve been using Google Search on websites hides a lot of material. One of the problems is that if nothing comes back, Google doesn’t say so, and nothing notifies the user that their search completed with nothing found. So here’s what we can do about it.
The GSearchControl object has a “setNoResultsString” function. You would think this would be your friend. For some reason, I couldn’t get this to work.
What I did was use the “setSearchCompleteCallback” function. Here’s my example code. What this does is defines an anonymous function which finds out how many results came back, and if it was zero, tell the user.
var foo;
var found = 0;
searchControl.setSearchCompleteCallback(foo, function() {
// determine search results (how many)
$('.gsc-stats').each(function() {
var v = $(this).html();
// v should look something like (#)
var close = v.indexOf(")");
found = v.substring(1, close);
// okay, so now we know how many things google found
// what do i do with that?
if (found == 0) {
$('.gsc-resultsRoot').html("No results found.");
}
});
}
);
To be a bit more verbose about this… foo doesn’t matter. The .gsc-stats container is where Google puts something like (0) or (2), which is the number of results found. As far as I can tell, this is the only place Google says “here’s how many I found.” So I use jQuery to parse that value. If it’s 0, I find this other container .gsc-resultsRoot and replace it’s contents with “No results found.”
Obviously, you could do all kinds of things with this function. “setSearchCompleteCallback” gives you a way to do something when the search is complete, successful or not. From there you can do some crazy stuff.