Blog

Or search support forum

What's Global Moxie?

Global Moxie is the hypertext laboratory of Josh Clark, whose projects include the Big Medium web content management system. Josh creates web applications and websites from his multimedia studio in Paris, France.

What's Big Medium?

Big Medium is flexible, easy-to-use server software for creating and editing websites directly from your browser. Check out the features or download now.

Moxiemail

Enter your e-mail to receive occasional updates:

OpenSearch: Add Your Site to Browsers’ Built-in Search

Posted Aug 30, 2007 (updated Feb 27, 2008)

Faithful reader, I have no doubt that there are moments—daily, perhaps—when you think, “Hey, I sure wish I could lay my hands on that witty/insightful/subtle comment that Josh made about topic X.” ....Or, y’know, maybe not.

In the unlikely event that this urge should one day descend upon you, I want to make it easy for you to scratch that itch as quickly as possible. If you’re reading this in Firefox 2 or Internet Explorer 7, you can now search this site right from your browser’s built-in search box.

This little bit of magic is achieved through an elegant little format called OpenSearch, mixed with Big Medium’s built-in site search. If you’re so inclined, you can do it for your own site, too. Herewith, a tutorial.

Collect your search details

Search - Firefox

The central element that makes this fandango tick is a XML text file that you place on your server. This file gives browsers three important pieces of information:

  • Name of your search.
    This is how your site’s search will appear in the browser’s search field, so it should be short and sweet, typically the name of your site. I use: “Global Moxie”

  • Search description.
    A brief description of your search and what it does. I use: “Search Global Moxie for info about Big Medium.”

  • Search URL.
    The URL location of your search engine, with the placeholder {searchTerms} representing the text to search.

Assemble your search URL

Sorting out the search URL for a Big Medium-powered site takes a little bit of rummaging, but it’s not difficult. First identify the base URL location: It’s the bm-search.cgi file in your installation’s moxiebin directory. For example:

http://example.com/cgi-bin/moxiebin/bm-search.cgi

A second element tells Big Medium what site you’re searching. This gets added, along with a question mark, to the URL like so ?bms=1:

http://example.com/cgi-bin/moxiebin/bm-search.cgi?bms=1

To find out what number to use for your site, go to a page that displays the Big Medium search form (generated with the <%search%> widget), and do a quick search. In the URL of the results page, you’ll see the bms= value. That’s the number to use.

The last element is the search query itself, and it’s handled like so, separated from the rest of the URL with a semi-colon: ;bmq={searchTerms}

Putting it all together, we get:

http://example.com/cgi-bin/moxiebin/bm-search.cgi?bms=1;bmq={searchTerms}

Create an OpenSearch XML file

With that info in hand, you’re ready to build your OpenSearch XML file. Here’s the format:

Search - IE7 add search
Adding a new search engine in IE7. Click to enlarge.
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">

    <ShortName>SEARCH-NAME</ShortName>
    <Description>SEARCH-DESCRIPTION</Description>
    <Url template="SEARCH-URL"

         type="text/html" method="GET" />
    <Image height="16" width="16" type="image/vnd.microsoft.icon">http://beta.bigmedium.com/favicon.ico</Image>
    <OutputEncoding>UTF-8</OutputEncoding>
    <InputEncoding>UTF-8</InputEncoding>
</OpenSearchDescription>

You can just copy this verbatim and replace SEARCH-NAME, SEARCH-DESCRIPTION and SEARCH-URL with the name, description and URL described in the previous section. Be careful: This is a XML file, so if your name, description or URL includes certain symbols, you need to replace them with the appropriate XML entities. Specifically: & ampersands (&amp;), quotation marks (&quot;), apostrophes (&apos;), < symbols (&lt;) and > symbols (&gt;)

Once you’ve built your XML file, upload it to your server at a location like this:

http://example.com/opensearch.xml

Let browsers know about your OpenSearch file

Both Firefox 2 and Internet Explorer 7 support “auto-discovery” of your custom search engine. When the browser detects your OpenSearch file, the browser’s search field highlights ever-so-subtly to indicate that there’s a new search engine available. Clicking the search field’s pulldown menu offers the option to add the search.

To trigger this behavior, you have to tip off the browser with a <link> tag in your page. In Big Medium, you add this link by going to “Settings>HTML Preferences” and then to the “Document and Page Header” screen. Add the following <link> tag to the “Additional HTML to include in header” field, replacing the title attribute with the short name you used in your OpenSearch file and the href attribute with the URL location of your OpenSearch file:

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

After rebuilding your pages, you’ll see that the search field in Firefox and Internet Explorer now offer the option to search your site. Hey, not too bad, but you have to admit... it’s still a little bit understated.

Be more direct

You can use a link to be bolder about proposing your site’s search engine to visitors. The following HTML snippet displays a link to supporting browsers (for now, Firefox 2 and IE7) allowing visitors to add your search to the browser. Add this to your Big Medium template(s) where you would like the link to appear:

<a href="#" id="addSearch" style="display:none">
    Add example.com search to your browser
</a>

<!-- edit the url below -->
<script type="text/javascript" language="javascript">
(function(){

    //edit this to point at your opensearch.xml file
    var url = "http://www.example.com/opensearch.xml";

    var add = function(ev){
        window.external.AddSearchProvider(url);
        Event.stop(ev);
    };
    if (  typeof window.external == "object"
       && typeof window.external.AddSearchProvider != "undefined" ) {
        var a = $('addSearch');
        Event.observe(a, 'click', add);
        a.toggle();
        a=null;
    }
})();
</script>

To customize that for your own site, just edit the “Add example.com” link text and edit this line to use the actual location of your OpenSearch file:

//edit this to point at your opensearch.xml file
var url = "http://www.example.com/opensearch.xml";

The JavaScript listed above uses the Prototype.js library which is included in all Big Medium pages. If you want to use this code snippet outside of Big Medium, be sure to include the Prototype library in your page.

Learn more

Check out these links to get the inside skinny on OpenSearch and its applications:

Tags: , , , ,

Want more? Recent blog entries...

Comments

2 comment(s) on this page (times are local Paris time). Add your own comment below.

Mar 10, 2008 9:26pm [ 1 ]

hello , thanks for the info m8 but iam having truble with my xml script i wonder if you could help me

here's my script: based on Google XML

<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
<ShortName>Angel Mp3 Search</ShortName>
<Description>Angel Search</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image height="16" width="16" type="webtv.6te.net/tracks/mpit.ico">http://webtv.6te.net/tracks/mpit.ico</Image>
<Url type="application/x-suggestions+json" method="GET" template="http://suggestqueries.google.com/complete/search?output=firefox&amp;client=firefox&amp;qu={searchTerms}"/>
<Url type="text/html" method="GET" template="http://angelmp3.uni.cc?search">
  <Param name="q" value="{searchTerms}"/>
  <Param name="ie" value="utf-8"/>
  <Param name="oe" value="utf-8"/>
  <Param name="aq" value="t"/>
  <!-- Dynamic parameters -->
  <Param name="rls" value="{moz:distributionID}:{moz:locale}:{moz:official}"/>
  <MozParam name="client" condition="defaultEngine" trueValue="firefox-a" falseValue="firefox"/>
</Url>
<SearchForm>http://angelmp3.uni.cc</SearchForm>
</SearchPlugin>

thanks

Mar 11, 2008 12:01am [ 2 ]

Sorry, I'm afraid that I don't use that particular <SearchPlugin> format. That format is intended for use in internal Mozilla projects only.

The OpenSearch format described above allows you to create a plugin that lets you add a new browser search engine from a web page. That's the purpose of this tutorial. If that's what you're trying to do, I recommend that you follow the steps described above to generate an OpenSearch XML file instead of the SearchPlugin format that you're using.

All best, Josh

Add a Comment

Don't be shy.

( )

( Use Markdown for formatting.)

Download Big Medium
Try it free for 30 days, or buy to unlock.

State of Josh's Brain