A preliminary study on OpenSearch

Source: Internet
Author: User
Tags add format definition version window

Many modern browsers have a search box on the right side of the address bar, and the default installation is Google search. As shown in the following figure

In fact, this is an application of opensearch, as long as the corresponding micro-format XML file, you can develop a corresponding search box. The basic XML format can be basically obtained by referencing the OpenSearch definition document. For example, a typical search XML file can be specified in this way.

<?xml version= "1.0" encoding= "UTF-8"?> <opensearchdescription xmlns=
"Http://a9.com/-/spec/opensearch" /1.1/">
    <InputEncoding>utf-8</InputEncoding>
    <shortname>shortname</shortname >
    <Description>Description</Description>
    <image type= "Image/vnd.microsoft.icon" > favicon</image>
    <url type= "text/html" template= "http://who.am.i/search?word={searchterms}"/>
  </OpenSearchDescription>

The above XML file makes it easy to understand that other than a fixed XML root, other definitions are literally understandable:

    • inputencoding Specifies the code for the search, depending on the actual site
    • ShortName This is a short name for the search, such as "Google search"
    • Description for this search box description, such as "Taobao shopping search – Only you can not imagine, no you can not find"
    • Image similar to the favicon of a Web page, used to identify search
    • Url This is the most important parameter that specifies the link for the search. It has many parameters, generally using the {searchterms} parameter to specify the search term. Parameter type= "Text/html" indicates that the page (the browser will jump to this page) will be opened using the corresponding default program if the other format (for example, type= "Application/rss+xml" will be opened using an RSS reader).

The XML format for writing OpenSearch is complete, and you can refer to its OpenSearch definition document for more information. The following to add this search to the page, basically can be divided into two ways. The page is added to the head by adding link tags (like RSS) and using Javascript to add (such as defining a button trigger). Joining the link tag is very simple, with the following format

<link rel= "Search" type= "Application/opensearchdescription+xml" href= "Http://who.am.i/search.xml" title= "
            ShortName "/>

Like RSS, rel and type are fixed, and we primarily specify href (the URL path for the XML above, using an absolute path, that is, http://) and title (the short title of the search). In this way, in the Explorer and Firefox open this page can see the corresponding menu, as shown in the picture

Adding more trouble with Javascript (maybe it's a lot of things now). We will mainly use the browser's extended functionality, in the Explorer has a window.external.AddSearchProvider parameter (detailed document). The typical invocation method is as follows

Window.external.AddSearchProvider ("Http://who.am.i/search.xml");

The link in the argument is the content in the link above. Can be used under Firefox

Window.sidebar.addSearchEngine (
    "Http://who.am.i/search.xml",/* engine URL * * "
    favicon.ico",  * icon URL
    /"ShortName",/* Engine name
    /"Description");/* Category name * *

Parameters and examples are described in the example code (official documentation). It is noteworthy that the Window.external.AddSearchProvider invocation method (details) for the "compatible" Explorer after the Firefox2 version has been made. So our corresponding Javascript code can be written like this (in order to be compatible with the previous version of Firefox2, add else if, if you feel it is unnecessary, you can not add)

function Addengine () {
    if (window.external  window.external.AddSearchProvider) {
        Window.external.AddSearchProvider ("Http://who.am.i/search.xml");
    } else if (Window.sidebar && window.sidebar.addSearchEngine) {
                window.sidebar.addSearchEngine (
                    "http ://who.am.i/search.xml ",
                    " Favicon.ico ",  /* Icon URL * *"
                    shortname ",/* Engine name * *
                    " Description "); /* Category name */
    }}

In this way, you can register this function to a link or button click event, you will jump out of a confirmation box, such as the user click to confirm, add to the browser search box.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.