Search code examples
javascripthtmlgoogle-apigoogle-custom-search

Google CSE: result page in a div of other page?


I want to add "Google CSE" but I do not want all the html and css created by Google, so looking on the internet, I almost found the solution:

<!-- Google custom search box Start -by BloggerSentral.com -->
<div class='cse' style='color:#000000;float:right;margin:6px 10px 0 0 ;'>
 <form action='http://www.google.com/cse' target="_blank" id='cse-search-box'>
  <input name='cx' type='hidden' value='015736343358803935205:m2shzyx2lrg'/>
  <input type='text' name='q' size='35' />
  <input name='ie' type='hidden' value='ISO-8859-1'/>
  <input type="submit" name="sa" value="Search" />
 </form>
 <script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script>
</div>
<!-- Google custom search box End -->

This code must be inserted in a bar already existing.

In my control panel "google cse", in "layout" section I put the option Only Results

Now when I search in this bar it opens a page:

http://www.google.com/cse+ id google cse +keyword searched

and it works.

But, I would like to see these results in a div (#content) of my website's page. Is this possible?


Solution

  • Boys I did! ;) I really hope that this proposal will be useful to many, also because I noticed that the questions about google cse receive very few answers ..

    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    
    google.load('search', '1'); 
    google.setOnLoadCallback(googlata);
    
    
    function googlata(){
    
    var customSearchControl = new google.search.CustomSearchControl('015736343358803935205:m2shzyx2lrg'); // change this to your unique ID  
    customSearchControl.setResultSetSize(20);
    customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF); // open results in new window
    
    var options = new google.search.DrawOptions();
    options.setAutoComplete(true);
    options.enableSearchResultsOnly()   
    customSearchControl.draw('cse',options); // set the results div id  
    
    //ottengo keyword searched  
    var link= self.location.href;
    var inizio = link.search("q=")+2;
    var fine= link.search("&");
    var keyword= (link.slice(inizio,fine)).replace('+',' ');
    //
    
    customSearchControl.setNoResultsString("<div style='padding:10px'><h2><b>Nessuno risultato per: &nbsp; "+keyword+" </b></h2><br><h3>Prova a cercare qualcosa di simile, siamo certi che potrai trovare cio' che ti interessa!</h3><br><br><br>Se il problema persiste non esitare a <a href='http://www.mywebsite.com/Contattaci'>contattarci</a>, provvederemo il prima possibile.</div>");
    
    customSearchControl.execute(keyword);    
    var newlink= link.replace('&sa=Search',''); 
    history.pushState({}, null, newlink);   
    }
    
    <div id="cse" style="width:100%;">Loading</div>
    

    In my case the results will be drawn in the div id="cse"

    With options.enableSearchResultsOnly() i drawn only results page, so my search bar is a normal search bar where i sent my data with method get, infact in my function googlata() i take data get by url (the keyword) and than i change my url because i want a clean url.

    it was very important to understand API 2.0 of google search, I recommend reading:

    https://developers.google.com/custom-search/docs/js/cselement-reference

    bye bye from italy! :D