我有Google自定义搜索(将来会成为网站搜索),我想使用Google提供的代码段:
<script>
(function() {
var cx = '...';
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>
<gcse:search></gcse:search>
我需要将其限制为某些网址。使用xml api时,可以使用参数as_sitesearch指定此过滤器..有没有办法用上面的代码执行此操作?
有了这个,我现在可以在加载搜索之前访问google.search.cse对象..但我还是不知道参数:
<script>
var myCallback = function() {
if (document.readyState == 'complete') {
// Document is ready when CSE element is initialized.
// Render an element with both search box and search results in div with id 'test'.
google.search.cse.element.render(
{
div: "test",
tag: 'search'
});
} else {
// Document is not ready yet, when CSE element is initialized.
google.setOnLoadCallback(function() {
// Render an element with both search box and search results in div with id 'test'.
google.search.cse.element.render(
{
div: "test",
tag: 'search'
});
}, true);
}
};
// Insert it before the CSE code snippet so that cse.js can take the script
// parameters, like parsetags, callbacks.
window.__gcse = {
parsetags: 'explicit',
callback: myCallback
};
(function() {
var cx = '007407192365638902354:eyxoavi7oa0';
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>
<style>
.gsc-control-cse * {
box-sizing:content-box;
}
</style>
答案 0 :(得分:0)
是的,你可以。
在搜索表单中使用隐藏字段:
<input type="hidden" name="as_sitesearch" value="your_url_here/directory">
答案 1 :(得分:0)
我有同样的问题。我能够将搜索限制在Google Site Search版本2中的特定文件夹的唯一方法是将其硬编码到元素中:
<gcse:search as_sitesearch="www.mydomain.com/site1"></gcse:search>
如果有一种方法可以通过JavaScript添加as_sitesearch,就像版本1一样,那就太棒了。
答案 2 :(得分:-1)
限制为某些网址:
<gcse:searchbox-only as_sitesearch="mydomain.com" resultsUrl="http://mydomain.com/search-results/" enableAutoComplete="false"></gcse:searchbox-only>
请务必将mydomain.com替换为您要限制的相应域。