一次显示多个GCSE

一次显示多个GCSE

本文介绍了一次显示多个GCSE.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的GCSE代码,如下所示:

Using the new GCSE code like so:

                    // 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,如下所示:

How do I go about including a second gcse tag on page and Assiging it a new CX:I've tried passing the gname for each to the url like so :

'//cse.google.com/cse.js?cx=' + cx + '&gname=sitesearch';

但无济于事...我已经阅读了说明文件;

but to no avail...I've read the documentation where it says;

我已阅读以下内容:同一页上的多个Google CSE(自定义搜索引擎)框但这是指旧版的GCSE而其中一个可接受的答案是使用iframe:如何具有多个Google自定义搜索字段在同一页面上

I've read this:Multiple Google CSE (Custom Search Engine) Boxes on Same Pagebut that refers to an older version of GCSEand this one where the accepted answer is use an iframe:How to have multiple Google Custom Search field on the same page

使用iframe似乎很笨拙,而不是正确的方法...?

Using an iframe just seems cludgy and not the right way to do it...?

推荐答案

使用自定义搜索元素v1,可以相对容易地完成该操作:

With Custom Search Element v1 it can be done relatively easy:

<script src='//www.google.com/jsapi' type='text/javascript'></script>
<script type='text/javascript'>
google.load('search', '1', {style: google.loader.themes.V2_DEFAULT});
google.setOnLoadCallback(function () {
  new google.search.CustomSearchControl('CSE_USER_ID:CDE_ID1').draw('cse');
  new google.search.CustomSearchControl('CSE_USER_ID:CDE_ID2').draw('cse2');
});
</script>

<div id='cse' style="width: 100%;">Loading</div>

<div id='cse2' style="width: 100%;">Loading</div>

演示:同一页面上有多个Google CSE
http://box.galeksic.com/cse.multiple-on-same-页面/

Demo: Multiple Google CSE on the same page
http://box.galeksic.com/cse.multiple-on-same-page/

JavaScript API参考(v1)
https://developers.google.com/custom-search/docs/js /cselement-reference

JavaScript API Reference (v1)
https://developers.google.com/custom-search/docs/js/cselement-reference

我尝试过使用v2,但是效果不是很好.

I've tried with v2, but wasn't very successful.

这篇关于一次显示多个GCSE.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:14