利用google的接口在网站嵌入搜索
如果要运行这个代码你需要先到http://code.google.com/apis/ajaxsearch/signup.html申请一个key替换代码"key=你在GOOGLE上申请到的KEY"中的key后的参数,就可运行了。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
</head>
<body>
<div>
<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=你在GOOGLE上申请到的KEY"
type="text/javascript"></script>
<script type="text/javascript">
function OnSearchLoad(searchStr)
{
// Create a search control
var searchControl = new GSearchControl();
var options = new GsearcherOptions();
options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
var WebSearch = new GnewsSearch();
searchControl.addSearcher(WebSearch,options);
WebSearch.setSearchCompleteCallback(this,GetResult,[WebSearch]);
//searchControl.addSearcher(new GblogSearch(),options); 这是blog搜索
//searchControl.addSearcher(new GnewsSearch(),options); 这是资讯搜索 还有其他好多类,可以查看google的api
searchControl.setResultSetSize(GSearch.LARGE_RESULTSET); //显示多少个,只能是4个和8个。。。。。。。
searchControl.draw(document.getElementById("searchcontrol"));
searchControl.execute(searchStr);
}
//GSearch.setOnLoadCallback(OnLoad);
function Search()
{
OnSearchLoad(document.all.searchKey.value);
}
function GetResult(searcher)
{
//返回结果,可以个性化的设置
var strContent = "";
if( searcher.results && searcher.results.length>0)
{
for(var i=0;i<searcher.results.length;i++)
{
var result = searcher.results[i];
strContent += "<br />"+result.title+"<br />来自:"+result.publisher+" <a href='"+result.unescapedUrl+"'>"+result.unescapedUrl+"</a><br />" +result.content+"<br />——"+result.publishedDate +"<br />";
}
}
document.all.Div1.innerHTML = strContent;
}
</script>
关键词:
<input type="text" id="searchKey" /> <span onclick="Search()">搜索</span>
<div id="searchcontrol" style="display: none;">
</div>
<div id="Div1">
</div>
</div>
</body>
</html>