加载Google自定义搜索结果后调用函数? [英] Call function after loading google custom search results?
问题描述
搜索结果在页面上呈现后,我基本上需要运行一些jQuery代码.我可以使用v1代码:
I basically need to run some jQuery code after the search results get rendered on my page. I can use either the v1 code:
<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'en', style : google.loader.themes.V2_DEFAULT});
google.setOnLoadCallback(function() {
var customSearchOptions = {};
var orderByOptions = {};
orderByOptions['keys'] = [{label: 'Relevance', key: ''},{label: 'Date', key: 'date'}];
customSearchOptions['enableOrderBy'] = true;
customSearchOptions['orderByOptions'] = orderByOptions; var customSearchControl = new google.search.CustomSearchControl(
'zzzzzzzzzzzz', customSearchOptions);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
customSearchControl.setAutoCompletionId('zzzzzz:zzzzzzz+qptype:3');
options.enableSearchResultsOnly();
customSearchControl.draw('cse', options);
function parseParamsFromUrl() {
var params = {};
var parts = window.location.search.substr(1).split('\x26');
for (var i = 0; i < parts.length; i++) {
var keyValuePair = parts[i].split('=');
var key = decodeURIComponent(keyValuePair[0]);
params[key] = keyValuePair[1] ?
decodeURIComponent(keyValuePair[1].replace(/\+/g, ' ')) :
keyValuePair[1];
}
return params;
}
var urlParams = parseParamsFromUrl();
var queryParamName = "q";
if (urlParams[queryParamName]) {
customSearchControl.execute(urlParams[queryParamName]);
}
}, true);
</script>
或v2代码:
<!-- Put the following javascript before the closing </head> tag. -->
<script>
(function() {
var cx = 'xxxxxxxxx';
var 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>
我尝试将函数调用放在代码中的不同位置,但无济于事.
I've tried putting my function call in various places in the code but no avail.
我有一个需要动态设置搜索结果出现在其中的容器的高度的函数,因此一旦加载结果,我就需要调用它,否则我的页面显示不正确.
I have a function that needs to dynamically set the height of the container the search results appear in, so once the results load I need to call it otherwise my page displays incorrectly.
此处是其v2 API文档的链接: https://developers.google.com/custom-search/docs/element#cse-element 和v1: https://developers.google.com/custom-search/docs/js/cselement-reference .我看不到只有在初始化时在搜索结果呈现时有回调的任何内容.似乎很疯狂,尽管不支持这样的事情.
Here is a link to their v2 API documentation: https://developers.google.com/custom-search/docs/element#cse-element and v1: https://developers.google.com/custom-search/docs/js/cselement-reference . I can't see anything where there is a callback upon search result rendering, only on initialization. Seems crazy though there isn't support for something like this.
这是我在v2中尝试过的方法:
Here is what I've tried with v2:
(function () {
var cx = 'xxxxxxxxx';
var 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;
gcse.onreadystatechange = gcse.onload = function () {
console.log("executed");
SetMainHeight(20);
};
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
控制台从不输出已执行".另外,我在控制台中注意到以下错误:
The console never outputs "executed". Also I notice in my console I have the following error:
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL http://www.google.com/cse?q=test&client=google-coop&hl=en&r=s&cx=xxxxx…s1%2Csr1&rurl=http%3A%2F%2Flocalhost%3A58257%2FSearch%3Fq%3Dtest#slave-1-1. Domains, protocols and ports must match.
不确定这是否重要,因为搜索功能仍然可以正常工作.我正在通过HTTP在本地主机上对此进行测试.
Not sure if that matters as the search function still works fine. I am testing this on localhost over http.
推荐答案
不幸的是,v2没有此功能,但是在v1中,您可以使用:
Unfortunately v2 doesn't have this ability, but in v1 you can use:
.setSearchCompleteCallback(object, method)
您可以在此页面上进行搜索.
这篇关于加载Google自定义搜索结果后调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!