Client-Side Search Engine

Jamie Jaworski is the author of five books on JavaScript, Java, and HTML, including Mastering JavaScript and JScript and Java 2 Unleashed.

Download Original Search.zip

In order to simplify searches and to illustrate a useful aspect of JavaScript, I have put together a client-side search engine that allows us to search through old SuperScripter columns.
I'll show you how to tailor this script to your own search needs. I also put together a Cool Tool that will help you to create a local search database.

The following form can be used to search through previous SuperScripter columns.
It uses a client-side script to perform the entire search; no server-side assistance is needed. The script contains a database of SuperScripter URLs, along with their descriptions and keywords. Type in one or more keywords (or select them from the list) and click Search to find the columns that are associated with those keywords. You can use the radio buttons to configure the search to return URLs that match either one or all of the supplied keywords. Play around with the search form to get an idea of how it behaves before going on to learn how it works.

Enter search keywords:

Select keywords from list: 

Match all keywords
Match any keyword 



 

When putting together a local search capability, there are a number of issues that you should consider:

In either of these cases, you may want to implement your search capability in client-side JavaScript. The easiest way to do this is to take the above search engine and to tailor it to your needs.

How the Search Form Works

The SuperScripter search form is a complete client-side solution for performing local searches. It works with versions 4.0 and later of Internet Explorer and Netscape Navigator. If you used your browser's View Source option to check out the search form's code, you were probably overwhelmed by the size of the script. Don't worry: most of the code is the search database, which consists of the following four arrays:

In addition to the above arrays, the script in the document's head consists of the following functions: In addition to the script in the document's head, one more script is embedded in the form's <SELECT> field. This script generates the <OPTION> tags of the field from the keywords array and sets up the search engine.

<SCRIPT LANGUAGE="JavaScript">
setupSearchEngine()
for(var i=0; i<keywords.length; ++i) {
document.writeln("<OPTION>"+keywords[i]+"</OPTION>")
}
</SCRIPT>

While all this may seem like a lot of code to read through, hang in there! The good news is that you can use the code as is with only minor modifications. All you need to do is to change the keywords, urls, urlDesc, and searchdb arrays to tailor the search form to work with your Web site.

Tailoring the Search Form

Tailoring the search form involves generating the values of the keywords, urls, urlDesc, and searchdb arrays and plugging that data into this template

You can then add any HTML that you want to dress up the template to work with your Web site. The template has the following section of code. Just replace the four array definitions with the data that pertains to your Web site.

The keywords array will consist of the keywords that your searches will support.
The urls and urlDesc arrays will contain your site's URLs and their descriptions.
The searchdb array will be an array of arrays that maps each keyword to an array of URLs.

// *** Replace the following four lines with the output of the Cool Tool ***
var keywords = new Array()
var urls = new Array()
var urlDesc = new Array()
var searchdb = new Array()

For large Web sites, you'll want to generate the above arrays by parsing the documents associated with a set of URLs, generating a database from that information, and constructing a few scripts that will put the database information in the required form.
For small local searches, such as the SuperScripter search form, you can build the arrays by hand using the local search Cool Tool.
The Cool Tool allows you to enter the URLs, their descriptions, and their keywords into a form for automatically generating the array definitions.

Cool Tool

Use the Add/Update button to add a URL description to the URL database. Click on a URL in the database and then click the Delete button to remove the URL from the database. You can also update a URL's description and click Add/Update to update the database information. When you are finished, click Generate Code to generate the array definitions to plug into the template


URL:
Description:
Keywords:

URL Database

Array Definitions

Download Original Version