我正在使用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
更多推荐
发布评论