jQuery(document).ready(function(){ /* Google Powered Site Search with jQuery Powered by Tzine & Google http://tutorialzine.com/2010/09/google-powered-site-search-ajax-jquery/ */ var config = { siteURL : 'sucht.it', // Change this to your site searchSite : true, type : 'web', append : false, perPage : 8, // A maximum of 8 is allowed by Google page : 0 // The start page } // Adding the site domain as a label for the first radio button: jQuery('#siteNameLabel').append(' '+config.siteURL); // Marking the Search tutorialzine.com radio as active: jQuery('#searchSite').click(); // Focusing the input text box: jQuery('#s').focus(); jQuery('#searchForm').submit(function(){ googleSearch(); return false; }); jQuery('#searchSite,#searchWeb').change(function(){ // Listening for a click on one of the radio buttons. // config.searchSite is either true or false. config.searchSite = this.id == 'searchSite'; }); function googleSearch(settings){ // If no parameters are supplied to the function, // it takes its defaults from the config object above: settings = jQuery.extend({},config,settings); settings.term = settings.term || jQuery('#s').val(); if(settings.searchSite){ // Using the Google site:example.com to limit the search to a // specific domain: settings.term = 'site:'+settings.siteURL+' '+settings.term; } // URL of Google's AJAX search API var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?'; var content = jQuery('#content'); jQuery.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){ var results = r.responseData.results; jQuery('#more').remove(); if(results.length){ // If results were returned, add them to a pageContainer div, // after which append them to the #content: var pageContainer = jQuery('
',{className:'pageContainer'}); for(var i=0;i
') .hide().appendTo(content) .fadeIn('slow'); var cursor = r.responseData.cursor; // Checking if there are more pages with results, // and deciding whether to show the More button: if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){ jQuery('
',{id:'more', html: '


'}).appendTo(content).click(function(){ googleSearch({append:true,page:settings.page+1}); jQuery(this).fadeOut(); }); } } else { // No results were found for this search. content.empty(); jQuery('

',{className:'notFound',html:''}).hide().appendTo(content).fadeIn(); } }); } function result(r){ // This is class definition. Object of this class are created for // each result. The markup is generated by the .toString() method. var arr = []; // GsearchResultClass is passed by the google API switch(r.GsearchResultClass){ case 'GwebSearch': arr = [ '

', '

',r.title,'


', r.content, '
',r.unescapedUrl,'', '
' ]; break; } // The toString method. this.toString = function(){ return arr.join(''); } } });