Click to See Complete Forum and Search --> : help to build a search engine


Bryan Tuffin
12-23-2002, 07:44 AM
Hello! Can any one help me? I would like to make a search engine in JavaScript that can search a text file. I would like a page with a message box and search button and a second box to list the results. I need the text file to contain the names of web pages and related key words plus the URL so I can double click on any result and go direct to that page. I would appreciate any help. Thanks.

AdamBrill
12-23-2002, 07:57 AM
I'm not aware of a way to load a .txt file in both netscape and IE. Here is how you can do it in IE:


<HTML>
<HEAD>
</HEAD>
<BODY>
<IE:DOWNLOAD id=oDownload style="BEHAVIOR: url(#default#download)">
<IE:DOWNLOAD>
<Script language=javascript>
oDownload.startDownload("search.txt",test);
function test(code)
{
//search through the code here.
}
</Script>
</BODY>
</HTML>



but, that doesn't work in Netscape. I don't think that there is a way to do it in Netscape. Sorry.

AdamGundry
12-23-2002, 12:16 PM
You can't easily import a .txt file without using a server-side script, but you can import a script file containing an array of your data, like this (in your head section):

<script type="text/javascript" src="search.js"></script>

In search.js, have an array like this:

var Names = new Array("Google","Webdeveloper Forum");
var Keywords = new Array("search engine","web forum");
var URLs = new Array("http://www.google.com","http://forums.webdeveloper.com");

Then you can perform a simple search like this:

var SearchKeyword = document.form.searchkeyword.value;
var Results = "Search Results: "

for (i=0;i<=Names.length;i++){
if (Keywords[i] == SearchKeyword){
Results = Results + Names[i];
}
}

This will take a keyword from the textbox "searchkeyword" in a form called "form" and search for it in the keywords array, adding any matches to the results string. You could probably improve this to use regular expressions to search the keywords, allowing partial matches.

As for a box of links, you could use a div (called "resultsdiv") and change the line Results = Results + Names[i]; to:

resultsdiv.innerHTML = resultsdiv.innerHTML + '<br><a href="' + URLs[i] + '">' + Names[i] + '</a>';

Good luck

Adam