在我的页面上呈现搜索结果后,我基本上需要运行一些jQuery代码。我可以使用v1代码:
<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>
我已尝试将我的函数调用放在代码中的各个位置但无效。
我有一个需要动态设置搜索结果出现的容器高度的函数,所以一旦结果加载我需要调用它,否则我的页面显示不正确。
以下是其v2 API文档的链接:https://developers.google.com/custom-search/docs/element#cse-element和v1:https://developers.google.com/custom-search/docs/js/cselement-reference。我只能在初始化时看到搜索结果呈现回调的任何内容。虽然没有这样的支持,但似乎很疯狂。
以下是我用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);
})();
控制台永远不会输出&#34;已执行&#34;。另外我注意到在我的控制台中我有以下错误:
不安全的JavaScript尝试使用以下网址访问以下网址的框架:来自网址http://www.google.com/cse?q=test&client=google-coop&hl=en&r=s&cx=xxxxx的空白... s1%2Csr1&amp; rurl = http%3A%2F%2Flocalhost%3A58257%2FSearch%3Fq%3Dtest#slave-1- 1。域,协议和端口必须匹配。
不确定这是否重要,因为搜索功能仍能正常工作。我正在通过http。
在localhost上测试这个答案 0 :(得分:12)
之前的解决方案对我不起作用,但这对我有用:
<script>
(function() {
window.__gcse = {
callback: myCallback
};
function myCallback() {
$('input.gsc-input').keyup(function(e) { if(e.keyCode == 13 || e.which == 13) { console.log('enter pressed'); }});
$('input.gsc-search-button').on('click', function(e) { console.log('clicked');});
}
var cx = '002806224466286757728:XXXXXXX';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = false;
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:search></gcse:search>
希望这有帮助
答案 1 :(得分:5)
答案 2 :(得分:0)
对于v2,例如,在将gcse对象(脚本)插入DOM之前,尝试将其设置为onload处理程序。
gcse.onreadystatechange = gcse.onload = function() {
//execute my code
};
基本上你最终得到:
<!-- 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;
gcse.onreadystatechange = gcse.onload = function() {
//execute your code here
};
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>