【问题标题】:Google CSE - search box that redirects to the page with resultsGoogle CSE - 重定向到带有结果的页面的搜索框
【发布时间】:2012-05-16 11:18:25
【问题描述】:

伙计们。

我对 Google CSE 手册有点困惑,无法理解它的一些基本功能。

所以,我创建了一个免费的自定义搜索配置文件,我得到的只是唯一的代码 sn-p,这个:

<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
  google.load('search', '1', {language : 'ru', style : google.loader.themes.V2_DEFAULT});
  google.setOnLoadCallback(function() {
    var customSearchOptions = {};
    var googleAnalyticsOptions = {};
    googleAnalyticsOptions['queryParameter'] = 'q';
    googleAnalyticsOptions['categoryParameter'] = '';
    customSearchOptions['googleAnalyticsOptions'] = googleAnalyticsOptions;  var customSearchControl = new google.search.CustomSearchControl(
      '001098741616107095995:xeqdxoqdue8', customSearchOptions);
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    customSearchControl.draw('cse');
  }, true);
</script>

这个 sn-p 在指定的 div 中创建 google 自定义搜索控件。这就是为什么这个 sn-p 可能会位于搜索结果页面中的原因,例如。 G。 www.site.com/search.html

但我要实现的是我网站每个页面侧边栏中的搜索框。用户无论身在何处都可以在该搜索框中输入搜索查询,然后直接进入结果页面 www.site.com/search.html 等待他的匹配集。

我该怎么做?

浏览手册对我没有任何影响,我遗漏了一些东西,但无法弄清楚到底是什么...... 谢谢!

【问题讨论】:

    标签: javascript google-custom-search


    【解决方案1】:

    google 搜索通过从 URL 参数获取搜索查询来工作。您只需要创建一个表单,将获取参数发布到您的结果页面。您的搜索字段的名称需要与 URL 栏中的名称匹配,通常是一个 q,例如 www.yoursite.com?q=your+search

    <form action="http://www.yoursite.com/resulspage" method="get">
    Search: <input type="text" name="q" />
    <input type="submit" value="Seacrh" />
    </form>
    

    【讨论】:

    • 感谢您的回答!我已将问题中的代码放在某个页面(domain.com/search)上,并提出了手动请求,例如 domain/com/search?q=php+cli+app 页面加载后,我只看到呈现的空搜索迅速的。没有执行任何搜索......我的代码似乎只能像一个小部件一样工作,没有任何外部查询可能。也许这是一个错误的代码?
    • 使用此方法可以启用auto-complete
    【解决方案2】:

    这里有一些对我有用的东西。 (如果我理解正确你的问题)

    src="https://www.google.com/jsapi"> .........

      customSearchControl.draw('google_search');
    
       <? if (isset($keyword)) { ?>     
         customSearchControl.execute('<?=$keyword?>'); <? } ?>    
       }
    google.setOnLoadCallback(OnLoad);
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 2013-07-22
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多