【发布时间】:2013-01-19 14:42:06
【问题描述】:
是否可以在 Google 自定义搜索的页面结果中添加下一个和上一个链接?我找到了这个帖子How to show next/previous links in Google Custom Search Engine paging links,但代码是针对旧版 Google 自定义搜索的。
较新的版本只是给你一个像<gcse:searchresults-only></gcse:searchresults-only> 这样的标签,一切都由此生成。有任何想法吗?谢谢
这就是搜索结果被拉入的方式..
<script>
(function() {
var cx = 'xxxxxx:xxxxx',
gcse = document.createElement('script');
gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchresults-only></gcse:searchresults-only>
【问题讨论】:
-
第一步可能是添加一个 gname 属性到您的 searchresults-only 元素,以便您可以在 javascript 中访问它。 IE:
然后你可以通过 google.search.cse.element.getElement('pickaname') 访问 CSE -
另外,我不会使用您发布的链接中的代码;它依赖于可能更改的未记录参数。我希望更好的方法是简单地生成您自己的分页并跟踪它们在您自己的哪个页面。您可以使用 searcher.gotoPage(page) 函数来更改页面。我唯一不确定的是如何从我的其他评论中提到的 CSE 元素中获取 searcher 对象,但您可以尝试 gsce_element.getWebSearcher() (其中 gsce_element 是 getElement 调用的结果)
-
最后,你不妨使用 HTML 风格的标签。参考:developers.google.com/custom-search/docs/…developers.google.com/custom-search/docs/element#html5developers.google.com/web-search/docs/reference#_class_GSearch 就个人而言,我坚持使用 V1,直到 V2 的内容得到更好的记录为止。
标签: javascript search google-custom-search