Setting Site Restriction to Google Ajax Search API

You can set site restriction to Search Engine created through Google Ajax Search API. setSiteRestriction() is the method using which you can restrict your search to specific site. Method setSiteRestriction('site-address'') can only restrict your search to one site only. If you want to restrict your search result to more than one sites then this way will not work!

You cannot do like this:


websearch.setSiteRestriction("site-address1.com");
websearch.setSiteRestriction("site-address2.com");

For restricting your search to multiple Sites, you need to use Google Custom Search Engine with Google Ajax Search API. Create Custom Search Engine with sites you want to include and get the Unique ID of that Search Engine.


<script src="http://www.google.com/jsapi?key=ajaxsearchAPI" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
google.load('search', '1');
function OnLoad() {
// Create a search control
var searchControl = new google.search.SearchControl();
// Add in a full set of searchers
var websearch   = new google.search.WebSearch();
searchControl.addSearcher(websearch);
// Method One. For one website only
// websearch.setSiteRestriction("satya-weblog.com");
// Method Two - For set of sites. Supply Custom Search Unique ID
websearch.setSiteRestriction("sdfksdfksjdflksdjflskdjf");
// Attach the result to 'mysearch''
searchControl.draw(document.getElementById("mysearch"));
// execute an inital search
searchControl.execute("");
}
google.setOnLoadCallback(OnLoad);
//]]>
</script>

<div id="mysearch">Loading</div>

Remember you need to get the Ajax Search API key for your website. Google Ajax Search API is mentioned at key=ajaxsearchAPI. This is not a valid search API. Likewise Custom Search Unique Id is also not a valid Id.!

  • # 1 - by Dan

    Thanks for this info…….

  • # 2 - by Dan

    Thanks for this info…….

Comments are open for an year period. Please, write here on Facebook page.