9.19.2007

nifty ajax search

i ran across google ajax search and thought that it'd be a great way to add search to this blog. here's what i wanted:

  • results scoped only to this blog
  • optional full google search results as well
  • no modification to the blogger template

here's how i got it:

  1. sign up for a key
  2. add an "HTML/JavaScript" page element to the blog
  3. give it a title and set the content to:
    <script src="http://www.google.com/jsapi?key=$YOUR_KEY" type="text/javascript"></script>
    <script type="text/javascript">
        google.load("search", "1");
    
        function OnLoad() {
    
          var searchControl = new google.search.SearchControl();
    
          var siteSearch = new GblogSearch();
          var options = new GsearcherOptions();
          options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
          siteSearch.setUserDefinedLabel("this blog");
          siteSearch.setSiteRestriction("$YOUR_BLOG_ADDRESS");
          searchControl.addSearcher(siteSearch, options);
         
          siteSearch = new GwebSearch();
          options = new GsearcherOptions();
          options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);
          siteSearch.setUserDefinedLabel("web");
          searchControl.addSearcher(siteSearch, options);
    
          searchControl.draw(document.getElementById("searchcontrol"));
        }
        google.setOnLoadCallback(OnLoad);
    </script>
    <div id="searchcontrol">Loading...</div>

as long as you replace $YOUR_KEY with the key you got in step one and $YOUR_BLOG_ADDRESS with the address of your blog, you should have search that's exactly like the search on this page. enjoy.

No comments: