如何将 Google 自定义搜索限制为特定 URL?
网页上有一个很长的公司名称列表。
每个名称都是指向位于列表页面下方的个人资料页面的链接。
因此,列表页面位于此处
http://example.com/companies/
,个人资料位于此处
http://example.com/companies/companyOne
http://example.com/companies/companyTwo
http://example.com/companies/companyThree
我想在该列表页面上实现 Google 自定义搜索框,以便访问者可以输入查询并仅显示相关的公司名称。我已经为搜索结果创建了一个单独的页面,在这里:
http://example.com/companies/results
所以我想基本上,我想将搜索限制到该单个列表页面:
http://example.com/companies/
我已经搜索了代码和文档,但我有权利现在返回网站所有页面的结果。并且 setSearchStartingCallback 永远不会被调用。
(抱歉,我无法提供实时 URL,这是一个私人测试版。)感谢您的任何想法!
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'fr', style : google.loader.themes.MINIMALIST});
google.setOnLoadCallback
(
function()
{
var customSearchControl = new google.search.CustomSearchControl('MY_GOOGLE_ID_THINGIE');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.setSearchStartingCallback
(
this,
function(control, searcher, query)
{
alert("setSearchStartingCallback");
searcher.setQueryAddition("inurl:www.example.com/companies/");
searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS,{"as_sitesearch": "www.example.com/companies/"});
}
)
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
customSearchControl.setAutoCompletionId('MY_GOOGLE_ID_THINGIE+qptype:1');
options.enableSearchboxOnly("http://www.example.com/companies/results");
customSearchControl.draw('cse-search-form', options);
}
,true
);
</script>
On a web page is a very long list of company names.
Each name is a link to a profile page located beneath the list page.
So the list page is located here
http://example.com/companies/
And profiles are here
http://example.com/companies/companyOne
http://example.com/companies/companyTwo
http://example.com/companies/companyThree
I want to implement a Google Custom Search box on that list page, so that a visitor could enter a query and be presented with only the relevant company names. I have created a separate page for the search results, here:
http://example.com/companies/results
So I guess basically, I want to limit the search to that single list page:
http://example.com/companies/
I've searched for code and documentation but what I have right now returns results from all the pages of the website. And the setSearchStartingCallback is never called.
(Sorry I cannot provide live URLS, this is a private beta.) Thanks for any ideas!
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'fr', style : google.loader.themes.MINIMALIST});
google.setOnLoadCallback
(
function()
{
var customSearchControl = new google.search.CustomSearchControl('MY_GOOGLE_ID_THINGIE');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.setSearchStartingCallback
(
this,
function(control, searcher, query)
{
alert("setSearchStartingCallback");
searcher.setQueryAddition("inurl:www.example.com/companies/");
searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS,{"as_sitesearch": "www.example.com/companies/"});
}
)
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
customSearchControl.setAutoCompletionId('MY_GOOGLE_ID_THINGIE+qptype:1');
options.enableSearchboxOnly("http://www.example.com/companies/results");
customSearchControl.draw('cse-search-form', options);
}
,true
);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用这个
Use this