GoogleAPIBook
搜尋控制容器顧名思義就是用來裝載搜尋功能的容器,
所有搜尋功能的物件都必須放入這個容器中才能運作。
換言之,
Google AJAX Search API最基礎的物件就是搜尋控制容器。
搜尋控制容器目前有兩種物件可以使用,
它們分別是google.search.SearchControl和google.search.SearchForm,
本文將先介紹google.search.SearchControl物件。

google.search.SearchControl是建立搜尋控制容器時,
第一種可以使用的物件。
google.search.SearchControl有許多函式可以幫助我們使用Google公司的搜尋服務,
不過在使用這些函式之前,
必須先建立這個搜尋控制容器物件。
要建立google.search.SearchControl物件,
可以使用下列語法。

var searchControl = new google.search.SearchControl();
google.search.SearchControl(searcher,opt_options) 這個函式可以將搜尋器加入搜尋控制容器中。
搜尋控制容器在最後執行搜尋動作的階段,
會針對它所擁有的搜尋器進行相對應的搜尋與顯示搜尋結果的動作。
google.search.SearchControl共可以帶入兩個引數,
以下為其說明。
searcher – 指定搜尋器的種類,目前Google公司提供的搜尋器種類如下表所示的。

image
opt_options – 非必填引數。這個引數可以針對搜尋器進行外觀與行為的細部設定。

最後,
有興趣的朋友可以根據上述函式撰寫如下的範例程式碼,
這樣就可以製作出一個具有多項搜尋功能的搜尋列喔。
<html>
<head>
<title>My Google AJAX Search API Application</title>
<script src="http://www.google.com/jsapi?key=金鑰" type="text/javascript">
</script>
<script language="Javascript" type="text/javascript">
google.load("search", "1");
function OnLoad() {
// 建立搜尋控制容器
var searchControl = new google.search.SearchControl ();
// 將所有搜尋器加入搜尋控制容器
searchControl.addSearcher(new google.search.WebSearch ());
searchControl.addSearcher(new google.search.LocalSearch ());
searchControl.addSearcher(new google.search.VideoSearch ());
searchControl.addSearcher(new google.search.BlogSearch ());
searchControl.addSearcher(new google.search.NewsSearch ());
searchControl.addSearcher(new google.search.BookSearch ());
searchControl.addSearcher(new google.search.ImageSearch ());
searchControl.addSearcher(new google.search.PatentSearch ());
// 將搜尋結果顯示在網頁中
searchControl.draw(document.getElementById("searchcontrol"));
}
google.setOnLoadCallback(OnLoad);
</script>
</head>
<body>
<div id="searchcontrol">Loading...</div>
</body>
</html>
PS:如果網友對Google API有興趣,
也可以參考我所撰寫的【Google APIs程式工具錦集】一書。


arrow
arrow
    全站熱搜

    賽拉維‧柯南 發表在 痞客邦 留言(4) 人氣()