How do browsers' Search boxes work? [closed]

半世苍凉 提交于 2019-12-03 21:27:15
splattne

You can create so called "Search Providers" for your sites. You should have a search page on your site which accepts the search keywords as query string in your URL, like

  http://www.example.com/search?q=meaning+of+life

This should work work Google Custom Search as well.

You'll have to create a special XML file (call it SearchProvider.xml, for example) and put it on your web server:

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
   <ShortName>Example Search Provider</ShortName>
   <Description>Finds answers to the most important question of the universe</Description>
   <InputEncoding>UTF-8</InputEncoding>
   <Url type="text/html" template=" http://www.example.com/search?q={searchTerms}"/>
</OpenSearchDescription>

Then, you'll need to insert a special link tag in your page's header section:

 <link title="Example Search Provider" rel="search"
     type="application/opensearchdescription+xml"
     href="http://www.example.com/SearchProvider.xml" />

You could also insert a link to your page, which allows your users to add the search provider to the browser:

<a href="#"
   onclick="javascript:window.external.AddSearchProvider('http://www.example.com/SearchProvider.xml');">
Example Search Provider</a>

Browser search boxes are implemented in a technology called OpenSearch. See: http://www.opensearch.org/ (site currently down?)

Mozilla has a good page which explains how to implement this for their browsers: https://developer.mozilla.org/en/Creating_OpenSearch_plugins_for_Firefox While there are a few Mozilla-specific details there, that page can serve as a good starting point for cross-browser implementation.

Add auto-complete to the search box is a bit trickier. First add the auto-complete query URL as described by Mozilla. Then you must craft a response on your server which conforms to what the various browsers expect.

Take a look at what Google returns for the different browsers they support:

* Firefox: http://suggestqueries.google.com/complete/search?client=firefox&hl=en-US&q=xmarks
      o Content-Type: text/javascript
      o Response body: ["xmarks",["xmarksthaspot","xmarksthescot","foxmarks safari","xmark.com","gmarks firefox","x marks foxmarks","xmarksthespot","xmarks ie","foxmarks addon","foxmarks for ie"]] 
* Safari: http://suggestqueries.google.com/complete/search?client=safari&hl=en-US&q=xmarks
      o Content-Type: application/json
      o Response body: ["xmarks",[["xmarksthaspot","18,400 results","0"],["xmarksthescot","196,000 results","1"],["foxmarks safari","148,000 results","2s"],["xmark.com","336,000 results","3s"],["gmarks firefox","50,700 results","4s"],["x marks foxmarks","13,500 results","5s"],["xmarksthespot","20,500 results","6"],["xmarks ie","96,400 results","7"],["foxmarks addon","210,000 results","8s"],["foxmarks for ie","191,000 results","9s"]]]
* Others: http://suggestqueries.google.com/complete/search?client=ie&hl=en-US&q=xmarks
      o Content-Type: text/javascript
      o Response body: I'm not sure it's relevant. It's essentially the exact same format as Safari above, but it's wrapped by a JavaScript call to window.google.ac.h(). I'm not 100% certain, but that looks like the callback to their HTML-page version of auto-completion and suggests to me that they don't really support opensearch auto-completion in anything but Firefox and Safari.

It probably depends on the browser to browser, but with Firefox, it's simple to customize it: see Search Bar and How To Easily Add A Custom Search Engine To Your Firefox Search Bar.

Microsoft provide a tool to add custom search providers to IE, and the Add to Search Bar extension allows you to do the same with Firefox.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!