2011-02-16 50 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