Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Custom Search on submit

I would like to customize my search form. I am using Google Search Service and have it linked to my domain and so on.

I chose the two column layout in the Control Panel, but however, I want to do something onSubmit of the form.

So I tried to put an actionlistener in jQuery into the form, however does not work.

Then I thought google certainly provides something for that. And yes they do. It is called:

setOnSubmitCallback()

http://code.google.com/apis/websearch/docs/reference.html

Unfortunately I dont get it.

So far I have:

google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});

                    function initialize()
                    {
                        var searchControl = new google.search.CustomSearchControl('017998360718714977594:j6sbtr-d6x8');
                        searchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);

                        var options = new google.search.DrawOptions();
                        options.setSearchFormRoot('cse-search-form');

                        searchControl.draw('cse', options);
                    }

                    google.setOnLoadCallback(initialize);

So i have two divs: #cse-search-form for the form and #cse for the results

#cse is in another div #searchResults, that is hidden and here it comes:

I want to open #searchResults in a dialog from jQuery UI.

$("#searchResults").dialog( { minWidth: 750, minHeight: 750 } );

Which will result into:

.setOnSubmitCallback(function() {
    $("#searchResults").dialog( { minWidth: 750, minHeight: 750 } );
} );

So my problem now is, where and on what do I have to put the setOnSubmitCallback?

I cannot put it on google.search.Search or CustomSearchControl as it is stated in the documentation. ANd I cannot call it in the onLoadCallback so it is very strange for me. Cannt figure out how to do that.

I hope somebody has some more experience for the google search and could help me out with a solution.

Thank you very much in advance.

like image 888
Richard Avatar asked Apr 30 '11 22:04

Richard


1 Answers

NOTE: the code below is using something Google deprecated. Use this instead: http://code.google.com/apis/customsearch/v1/overview.html

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Hello World - Google  Web Search API Sample</title>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
    <script src="https://www.google.com/jsapi" type="text/javascript"></script>
    <script language="Javascript" type="text/javascript">
    //<![CDATA[
    google.load('search', '1');
    google.load("jquery", "1.5.2");
    google.load("jqueryui", "1.8.12");

    function OnLoad() {

        var searchComplete = function(searchControl, searcher){
            $('#searchResults').dialog({modal: true, width: 700, height: 400, position: [50, 50]});
            for (result in searcher.results) {
                var content = searcher.results[result].content;
                var title = searcher.results[result].title;
                var url = searcher.results[result].url;
                $('#searchResults ul')
                    .append($('<li></li>')
                        .append($('<a/>').attr('href', url).text(title))
                        .append($('<p/>').text(content)));
            }
        };

        // called on form submit
        newSearch = function(form) {
          if (form.input.value) {
            // Create a search control
            var searchControl = new google.search.SearchControl();

            // Add in a set of searchers
            searchControl.addSearcher(new google.search.WebSearch());
            searchControl.addSearcher(new google.search.VideoSearch());

            // tell the searchControl to draw itself (without this, the searchComplete won't get called - I'm not sure why)
            searchControl.draw();

            searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);           
            searchControl.setSearchCompleteCallback(this, searchComplete);
            searchControl.execute(form.input.value);
          }
          return false;
        }

        // create a search form without a clear button
        // bind form submission to my custom code
        var container = document.getElementById("searchFormContainer");
        this.searchForm = new google.search.SearchForm(false, container);
        this.searchForm.setOnSubmitCallback(this, newSearch);
    }
    google.setOnLoadCallback(OnLoad);

    //]]>
    </script>
  </head>
  <body>
    <div id="searchFormContainer">Loading</div>
    <div id="searchResults" title="Search Results">
        <ul></ul>
    </div>
  </body>
</html>
like image 154
Milimetric Avatar answered Nov 15 '22 11:11

Milimetric