在Google CSE v2中获取查询字符串(Get query string in Google CSE v2)

编程入门 行业动态 更新时间:2024-10-23 23:24:17
在Google CSE v2中获取查询字符串(Get query string in Google CSE v2)

我正在使用Google CSE v2,我需要获取用户输入的查询。 问题是它是ajax,查询不在url中。

有谁知道解决方案?

谢谢

I am using Google CSE v2, and I need to get the query that the user entered. The problem is that it is ajax, and the query is not in the url.

Does anyone know a solution?

Thanks

最满意答案

首先,当您创建搜索框时,您需要为其指定一个'gname'属性,以便您可以在javascript中识别它,如下所示:

<gcse:searchbox gname="storesearch"></gcse:searchbox> <gcse:searchresults gname="storesearch"></gcse:searchresults>

或者,如果您使用的是html5样式标记(除非您有理由,否则应该使用):

<div class="gcse-searchbox" data-gname="storesearch"></div> <div class="gcse-searchresults" data-gname="storesearch"></div>

(将'storesearch'替换为您要用于识别此自定义搜索的任何名称。)

有关详细信息,请访问: https : //developers.google.com/custom-search/docs/element#supported_attributes

然后,您可以访问自定义搜索元素并获取当前查询,如下所示:

var cseElement = google.search.cse.element.getElement('storesearch'), query = cseElement.getInputQuery();

或者如果您不再需要对元素的引用,显然可以组合成一行:

var query = google.search.cse.element.getElement('storesearch').getInputQuery();

该部分的文档位于: https : //developers.google.com/custom-search/docs/element#cse-element

First off, when you create the search box, you need to give it a 'gname' attribute so you can identify it in your javascript, like so:

<gcse:searchbox gname="storesearch"></gcse:searchbox> <gcse:searchresults gname="storesearch"></gcse:searchresults>

Or, if you're using the html5 style tags (which you should unless you have a reason not to):

<div class="gcse-searchbox" data-gname="storesearch"></div> <div class="gcse-searchresults" data-gname="storesearch"></div>

(Replace 'storesearch' with whatever name you want to use to identify this custom search.)

More info on that here: https://developers.google.com/custom-search/docs/element#supported_attributes

Then, you can access the custom search element and get the current query like so:

var cseElement = google.search.cse.element.getElement('storesearch'), query = cseElement.getInputQuery();

or if you don't need the reference to the element anymore, obviously that could be combined into one line:

var query = google.search.cse.element.getElement('storesearch').getInputQuery();

The docs for that part are here: https://developers.google.com/custom-search/docs/element#cse-element

更多推荐

本文发布于:2023-08-07 05:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   CSE   Google   string   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!