2011-02-16 70 views
3

我想爲我的網站添加搜索。我想使用谷歌自定義搜索... 我需要的是在母版頁的標題中有一個搜索框,然後在用戶搜索時單獨搜索結果頁面。將搜索整合到asp.net MVC網站的標題/主頁面

我該如何實現這個?,因爲谷歌給出的代碼片段似乎是我想要的結果頁面?我也可以將它整合到一個仍在開發中的網站嗎?

回答

0

我認爲在創建Google自定義搜索時選擇iframe選項是實現它的方法。 它將代碼分隔成一個搜索框,結果頁面

2

按照Google的建議在結果頁面上實現代碼。然後在你的母版頁添加以下

<form id="searchForm" method="get" action="/url-to-your-search-page/" > 
    <input id="search" name="search" class="Search" type="text" /> 
    <input type="submit" value="search" /> 
</form> 

的谷歌定製搜索代碼的搜索頁面上會採取其他的事情。

更新

您可以使用下面的JavaScript在搜索結果頁面的底部提取搜索查詢字符串並執行了谷歌搜索

<script type="text/javascript"> 
    google.load('search', '1'); 
    function OnLoad() { 
     var s = window.location.search; 
     if (s.indexOf('search=') >= 0) { 
      s = s.substring(s.indexOf('search=') + 7); 
      if (s.indexOf('&') >= 0) { 
       s = s.substring(0, s.indexOf('&')); 
      } 
      s = decodeURIComponent(s.replace('+', ' ')); 
     } 
     else { 
      s = ""; 
     } 
     var customSearchControl = new google.search.CustomSearchControl('your-custom-search-id'); 
     customSearchControl.draw('content'); 
     customSearchControl.execute(s); 
    } 
    google.setOnLoadCallback(OnLoad); 
</script> 

不要忘了,包括您的在標題中的腳本參考

<script src="http://www.google.com/jsapi"></script> 
+0

這沒有爲我工作 - 任何想法?它會重定向到我的搜索頁面。什麼也沒有顯示 – raklos 2011-02-16 22:25:37