頂唔順 serendipity 個 search, 成日都 search 唔到野, 所以直按用 Google 既 AJAX Search。
<div id="searchcontrol"></div>
上面個 div tag 決定 search result 放係邊, default 會連個 search text input 同埋個 submit button, 但係我用
setSearchFormRoot 改左個位置, 放到去 <div id="searchFormContainer"></div> 度:
var drawOptions = new GdrawOptions();
drawOptions.setSearchFormRoot(document.getElementById("searchFormContainer"));
另外, 要限制個 google search 只可以 search orz.hk:
var siteSearch = new GwebSearch();siteSearch.setUserDefinedLabel("orz.hk");siteSearch.setSiteRestriction("orz.hk");
至於預設搜尋結果太嚴謹, 所以:
var options = new GsearcherOptions();
options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
咁就可以加落去個 search 度:
searchControl.addSearcher(siteSearch, options);
其餘既 HTML code:
[geshi lang=html]
[/geshi]
your key 就要去 Google AJAX Search API 度申請, 免費, 即時有, 仲要即刻出個 sample code 俾你直接放係自己網站度試, 連 key 都唔駛煩。以下係 templates/neo/google.js 全部 source code:
[geshi lang=java]
google.load(“search”, “1″);
var app;
var searchControl;
function OnLoad() {
app = new App();
}
function App() {
// Create a search control
searchControl = new google.search.SearchControl();
var siteSearch = new GwebSearch();
siteSearch.setUserDefinedLabel(“orz.hk”);
siteSearch.setSiteRestriction(“orz.hk”);
var options = new GsearcherOptions();
options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
searchControl.addSearcher(siteSearch, options);
var drawOptions = new GdrawOptions();
drawOptions.setSearchFormRoot(document.getElementById(“searchFormContainer”));
searchControl.draw(document.getElementById(“searchcontrol”), drawOptions);
var container = document.getElementById(“searchFormContainer”);
this.searchForm = new GSearchForm(false, container);
this.searchForm.setOnSubmitCallback(this, App.prototype.newSearch);
}
// called on form submit
App.prototype.newSearch = function(form) {
if (form.input.value) {
searchControl.execute(form.input.value);
}
return false;
}
google.setOnLoadCallback(OnLoad);
[/geshi]