2013-01-14 41 views
0

我目前正在使用google cse,并且我想知道它是否可以跳转到特定的搜索词。例如,如果我在我的页面上看到出版物这个词,并且页面上有一部分出版物,是否可以通过使用实施的Google搜索直接跳到本部分?Google CSE - 跳转到特定关键字

此刻每次搜索我都会将我引导到放置关键字的网页的开头。

问候马丁

回答

0

这是可能的,是的,但你必须做一些编码。如果您考虑Google搜索的工作方式,它会生成一个链接列表。它无法控制一旦你到达其他页面会发生什么。所以,你想在目标页面上发生的任何事情,你都必须自己编码。如果你真的想这样做,你可以这样设置:

首先,你会想使用V1 CSE,因为它提供了在搜索完成时设置回调函数的能力,简化的V2不会。见V1文件位置:https://developers.google.com/custom-search/docs/js/cselement-devguide

下面是从该网页的示例代码,修改,添加一个回调函数:

<!-- 
    copyright (c) 2012 Google inc. 

    You are free to copy and use this sample. 
--> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title>Google Custom Search Element API Example</title> 
    <script src="https://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load('search', '1'); 
     google.setOnLoadCallback(function(){ 
      customSearchControl = new google.search.CustomSearchControl().draw('cse'); 
     }, true); 
      customSearchControl.setSearchCompleteCallback(this, searchCompleteFn); 
    </script> 
    </head> 
    <body style="font-family: Arial;border: 0 none;"> 
    <div id="cse" style="width:100%;">Loading...</div> 
    </body> 
</html> 

然后你就可以创建一个回调函数,像这样:

function searchCompleteFn(control, searcher) { 
    //your code 
} 

在你的代码,你会想要获得cse div中的所有a.gs-title元素,并修改它们的href属性来添加用户的查询。这样,你的目的地页面就可以看到用户搜索的内容并采取适当的行动(滚动到适当的部分,突出显示关键字,无论你想要什么)。例如,如果现有的href是http://www.yoursite.com/somepath/somepage.html,则可以将其更改为http://www.yoursite.com/somepath/somepage.html#query=[user_query]。

[USER_QUERY]由control.getInputQuery给出()

最后,在你的目的地的网页你会JavaScript的检查中的location.hash查询参数,并采取适当的行动。

我猜这是比你感兴趣的方式更多的工作,但也许这对某人会有帮助。