问题描述
我正在尝试在同一页面上实施两个(不同的)Google CSE搜索框。问题是只有第一个实例正常工作。例如,标题中的网站范围搜索框,然后是某些页面上的第二个搜索框,在网站的狭窄仓库内搜索,等等。
I am trying to implement two (different) Google CSE search boxes on the same page. The issue is that only the first instance works properly. For example, a sitewide search box in the header, then on certain pages, a 2nd search box that searches within a narrow silo of the site, etc.
这不是'通过使用每个框的谷歌生成的代码,它们都可以正常工作,它们都获得相同的表单ID,这显然是无效的。这会导致Google水印品牌无法显示在方框#2中,并且某些自动/谷歌生成的变量也无法为第二个方框生成,例如ss参数(搜索会话)。
This doesn't work properly in that with using the google-generated code for each box, they both get the same form ID, which is obviously not valid. This causes the google watermark branding to fail to appear in box #2, and also some auto/google generated variables fail to generate for the 2nd box also, such as the ss parameter (search session).
现在,搜索本身可以在两个框中工作,例如用户搜索,用于更正结果页面,并显示正确的结果。
Now, the search itself works in both boxes, e.g. user searches, is taken to correct results page, and correct results are shown.
我只是想解决这个问题:如何让第二种形式获得不同的ID值(并且仍能正常工作 - 因为google的javascript会查找ID cse-search-box
)我尝试在< form>
元素,但这导致google.com javascript出现故障(因为我认为它只查找`cse-search-box')
I am just trying to resolve this issue: how can I cause the 2nd form to get a different ID value (and still work properly - as google's javascript looks for the ID cse-search-box
) I have tried adding a unique identifier to the ID and NAME attributes of the <form>
element, but that caused the google.com javascript to malfunction (as I believe it looks for `cse-search-box' only)
代码如下:
搜索框1:
<form id="cse-search-box" name="cse-search-box" class="search searchHeader" method="get" action="/search">
<input type="hidden" name="cx" value="partner-pub-0000000000000000:000" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" id="q" class="text_input" />
<input type="submit" name="sa" value="Search" class="submit" />
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>
搜索框2:
<form id="cse-search-box" name="cse-search-box" class="search searchWebDirectory" method="get" action="/search">
<input type="hidden" name="cx" value="partner-pub-0000000000000000:111" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" id="q" class="text_input" />
<input type="submit" name="sa" value="Search" class="submit" />
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>
推荐答案
来自:
From: http://www.google.com/cse/docs/cref.html
名为form的/ coop / cse / brand中的参数输出代码中的第一个getElementById。因此,使用将创建一个 cse-search-box
-targetted代码,同时给它 box2
将触发另一个ID。关键字 searchbox_demo
是保留的。
The parameter in /coop/cse/brand named "form" outputs the first getElementById in the code. As such, using http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en will create a cse-search-box
-targetted code, whilst giving it box2
will trigger on a different ID. The keyword searchbox_demo
is reserved.
如果您愿意,请试一试:。然而,考虑到你在做什么,如果我是你,我会切换到V1或V2 API。
Give it a go if you'd like to: http://jsfiddle.net/JTV6f/1/ . Considering what you are doing, however, if I were you, I'd switch to either the V1 or V2 APIs.
这篇关于同一页面上的多个Google CSE(自定义搜索引擎)框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!