views:

104

answers:

1

Hey guys,

I've implemented Google site search / Custom search for my website and it is all working and results are formatted and paging fine. But it never returns a count of how many results it found like it does when you search on Google About 1,660,000 results (0.16 seconds)

I was wondering if anyone had found anything to do this i can't find anything in there documentation.

<div id="cse" style="width: 100%;">Loading</div>
        <script src="http://www.google.com/jsapi" type="text/javascript"></script>
        <script type="text/javascript">
            google.load('search', '1', {language : 'en'});
            google.setOnLoadCallback(function() {
                var customSearchControl = new google.search.CustomSearchControl('GOOGLEIDGOESHERE');
                customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
                customSearchControl.setNoResultsString("No results found.")
                customSearchControl.draw('cse');   
            }, true);
</script>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
+2  A: 

You will need to use the SearchCompleteCallback and buried deep within the obfuscated javascript library, you will find the estimatedResultCount property. Here's a quick example that pops up an alert with the count. You can tailor this to meet your needs by using jquery to insert some html with the count in any format you like.

<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">

google.load('search', '1', {language : 'en'});
google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('GOOGLEIDGOESHERE');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    customSearchControl.setNoResultsString("No results  found.")
    customSearchControl.setSearchCompleteCallback(null, 
        function() { searchCompleteCallback(customSearchControl) });

    customSearchControl.draw('cse');   
}, true);


function searchCompleteCallback(customSearchControl) {

  alert(customSearchControl.e[0].g.cursor.estimatedResultCount);

}
</script>
Kevin Lewis
wow thanks that was hidden nicely by them. Which documentation did you read to find this out?
ozatomic