opensearch让你网站的搜索搬到浏览器

opensearch,分享搜寻结果的简单的格式。IE7+,以及FF2+都支持。让你网站的搜索服务搬到浏览器上面,不管实用不实用,反正不是很难,不防给网站加上。

一.创建opensearch.

1.可以能管IE8的专门网站www.ideadons.com创建

<?xml version=”1.0″ encoding=”UTF-8″ ?> 
<OpenSearchDescription xmlns=”http://a9.com/-/spec/opensearch/1.1/”>
<ShortName>搜索名称</ShortName> 
<Description>搜索描述</Description> 
<InputEncoding>UTF-8</InputEncoding>  //输入编码
<Url type=”text/html” template=”http://www.dansion.com/blog/q={searchTerms}” /> //搜索的地址,其中{searchTerms}是用户浏览器输入的
</OpenSearchDescription>

这是最简单的一个opensearch.还可以增加一些输入的建议的功能。

<?xml version=”1.0″ encoding=”UTF-8″ ?> 
<OpenSearchDescription xmlns=”http://a9.com/-/spec/opensearch/1.1/”>
<ShortName>搜索名称</ShortName> 
<Description>搜索描述</Description> 
<Contact>联系(邮件地址)</Contact> 
<Url type=”text/html” method=”get” template=”http://www.yoursite.com/search?q={searchTerms}” /> //搜索结果
<Url type=”application/x-suggestions+xml” template=”http://www.yoursite.com/suggest/ie.s?query={searchTerms}” /> //输入关健字建议,输入联想
<Url type=”application/x-suggestions+json” method=”GET” template=”http://www.yoursite.com/suggest/js.s?query={searchTerms}” /> //输入关健字建议,输入联想,针对FF
<Image height=”16″ width=”16″ type=”image/icon”>http://www.yoursite.com/favicon.ico</Image> 
<LongName>长名字</LongName> 
<Developer>开发者</Developer> 
<Attribution>归属</Attribution> 
<SyndicationRight>open</SyndicationRight> //默认open,还有limited,private,closed
<AdultContent>false</AdultContent> //是否仅对成人
<Language>en-us</Language>
<OutputEncoding>UTF8</OutputEncoding> //输出编码
<InputEncoding>UTF8</InputEncoding> //输入编码
</OpenSearchDescription>

opensearch 详细的标签说明 

输入提示的XML格式如下

<?xml version=”1.0″ encoding=”UTF8″ ?>
<SearchSuggestion version=”2.0″ xmlns=”http://opensearch.org/searchsuggest2″>
<Query>输入关健字</Query>
<Section title=”Yodao Web Search Suggestion”>
<Item>
<Text>相关提示</Text>
</Item>
</Section>
</SearchSuggestion>

json格式如下:

["输入关健字",["相关提示","相关提示","相关提示"]]

输入提示还可以更丰富,比如还可以增加图片,描述,链接,例如淘宝那样.

更多详情标签格式.

二.如何添加或让用户知道

创建并上传opensearch XML文件到到你的网站,再在需要提示的页面增加html头部添加以下代码。
<link rel=”search” type=”application/opensearchdescription+xml” href=”http://www.yoursite.com/search.xml” title=”搜索名字” />
这种方式只会在浏览器搜索框有提示,不过不明显。

还可以提供按扭让用户安装的js方法如下:(该js来自:https://developer.mozilla.org)

function installSearchEngine() {
 if (window.external && (”AddSearchProvider” in window.external)) {
   // Firefox 2 and IE 7, OpenSearch
   window.external.AddSearchProvider(”http://example.com/search-plugin.xml”);
 } else if (window.sidebar && (”addSearchEngine” in window.sidebar)) {
   // Firefox <= 1.5, Sherlock
   window.sidebar.addSearchEngine(”http://example.com/search-plugin.src”,
                                  ”http://example.com/search-icon.png”,
                                  ”Search Plugin”, “”);
 } else {
   // No search engine support (IE 6, Opera, etc).
   alert(”No search engine support”);
 }
}

ie8还可以用window.external.AddService(URL)方法实现.

Tags: , , ,

Leave a Reply