Google Ajax search


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script src="http://www.google.com/jsapi?key={google_search_api_key}" type="text/javascript"></script>
  2. <script language="Javascript" type="text/javascript">
  3. //<![CDATA[
  4. var query = '<?php if(isset($_GET['q'])) echo $_GET['q']; ?>';
  5. var noResultsStr = "{no_results_str}"
  6. google.load("search", "1");
  7. // Call this function when the page has been loaded
  8. function initialize() {
  9. // Dynamically load CSS to override defaults
  10. var options = new google.search.SearcherOptions();
  11. options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  12.  
  13. var siteSearch = new google.search.WebSearch();
  14. //siteSearch.setUserDefinedLabel("{tab_lbl}");
  15. siteSearch.setUserDefinedClassSuffix("siteSearch");
  16. siteSearch.setSiteRestriction("{google_cse}");
  17.  
  18. var searchControl = new google.search.SearchControl();
  19. searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
  20. searchControl.addSearcher(siteSearch, options);
  21. searchControl.setNoResultsString(noResultsStr)
  22.  
  23. // create a drawOptions object
  24. var drawOptions = new google.search.DrawOptions();
  25. // tell the searcher what input field to use
  26. drawOptions.setInput(document.getElementById("{search_input_id}"));
  27. // tell the searcher to draw itself in tabbed mode
  28. drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
  29. searchControl.draw(document.getElementById("searchcontrol"), drawOptions);
  30.  
  31. searchControl.execute(query);
  32. }
  33. google.setOnLoadCallback(initialize);
  34.  
  35. //]]>
  36. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.