Google CSE: results page in div of another page?

I want to add “Google CSE”, but I don’t want all the html and css created by Google, so looking on the Internet I almost found a 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 into an existing bar.

In my google cse control panel, under the layout section, I set the Results Only option

Now, when I search in this line, the page opens:

http://www.google.com/cse+ google search id cse +

and it works.

But I would like to see these results in the div (#content) of my website page. Is it possible?

+4
source share
1 answer

, !;) , , , , google cse .

<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>

div id="cse"

options.enableSearchResultsOnly() , - , get, infact googlata(), URL- ( ) url, URL.

API 2.0 google search, :

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

!: D

0

Source: https://habr.com/ru/post/1615427/


All Articles