【发布时间】:2015-10-12 17:59:27
【问题描述】:
像这样使用新的 GCSE 代码:
// google custom search engine for the whole site
(function() {
var cx = '*****************';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx + '&gname=sitesearch';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
和这样的元素:
<gcse:searchbox gname="sitesearch"></gcse:searchbox>
<gcse:searchresults gname="sitesearch"></gcse:searchresults>
如何在页面上包含第二个 gcse 标记并为其分配一个新的 CX: 我已经尝试将每个 gname 传递给 url,如下所示:
'//cse.google.com/cse.js?cx=' + cx + '&gname=sitesearch';
但无济于事... 我已经阅读了它所说的文档;
(可选)CSE 元素对象的名称。名称用于按名称检索关联的组件,或将搜索框组件与搜索结果组件配对。如果未提供,自定义搜索将根据网页上组件的顺序自动生成一个 gname。例如,第一个未命名的 searchbox-only 的 gname 为“searchbox-only0”,第二个的 gname 为“seachbox-only1”,以此类推。请注意,为两列布局中的组件自动生成的 gname 将是两列。以下示例使用 gname storesearch 将搜索框组件与 searchresults 组件链接: 这使得将搜索框分配给搜索结果变得很容易我似乎无法将 cx 代码附加到实际标签。
我读过这个: Multiple Google CSE (Custom Search Engine) Boxes on Same Page 但那是指较旧版本的GCSE 而这个接受的答案是使用 iframe: How to have multiple Google Custom Search field on the same page
使用 iframe 看起来很笨拙,而且不是正确的方法...... ?
【问题讨论】: