Important alert: (current site time 7/15/2013 1:08:25 PM EDT)
 

VB icon

A Search Engine using JavaScript! No external files or dependencies

Email
Submitted on: 5/1/2000 8:34:19 PM
By:  
Level: Advanced
User Rating: By 1 Users
Compatibility: JavaScript
Views: 33318
(About the author)
 
     This code allows you to search an amphorus database that is built into the page.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :A Search Engine using JavaScript! No external files or dependencies
'**************************************
The HTML is within the code
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A Search Engine using JavaScript! No external files or dependencies
' Description:This code allows you to search an amphorus database 
that is built into the page.
' By: 
'
' Inputs:You need to make the database (it is a hidden field)
The user needs to input a search string
'
' Returns:The pages, naturally
'
' Assumes:You need to know HTML
'
' Side Effects:None known, however, if you find any cantact me at:
thundersoft@hotmail.com
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1820&lngWId=14'for details.'**************************************

<html>
<title>JavaScript Search Engine Script with Descriptions</title>
<!--This code is originally from Infohiway.com, however, it has been modified-->
<body bgcolor=white>
<FORM NAME="database">
<INPUT TYPE="hidden" NAME="list" VALUE='your_first_page.htm~Your First Page Title|keywords for first file go here separated by spaces^First page description here!*http://you.can.use.full.urls.com/your_second_page.htm~Your Second Page Title|keywords for second file go here separated by spaces^Second page description here!*your_third_page.htm~Your Third Page Title|keywords for third file go here separated by spaces^Third page description here!*your_fourth_page.htm~Your Fourth Page Title|keywords for fourth file go here separated by spaces^Fourth page description here!*'>
</FORM>
<script language="JavaScript">
<!--
function Page(url,title,keywords,description) {
 while ((url.length > 0) && (url.charAt(0) == " ")) {
 url = url.substring(1,url.length);
 }
 this.url = url;
 while ((title.length > 0) && (title.charAt(0) == " ")) {
 title = title.substring(1,title.length);
 }
 this.title = title;
 this.keywords = keywords;
 this.description = description;
 return this;
}
function Database() {
 var pos = 0;
 while ((pos1 = amorphous.indexOf("~",pos)) != -1) {
 pos2 = amorphous.indexOf("|",pos1+1);
 pos3 = amorphous.indexOf("^",pos2+1);
 pos4 = amorphous.indexOf("*",pos3+1);
 if ((pos2 != -1)
 && (pos2 < pos3) && (pos3 < pos4)
 && (pos4 <= amorphous.indexOf("*",pos))) {
this[database_length++] = new Page(amorphous.substring(pos,pos1),
amorphous.substring(pos1+1,pos2),
amorphous.substring(pos2+1,pos3),
amorphous.substring(pos3+1,pos4));
pos = pos4+1;
 } else { // error reading amorphous database
if (pos+30 <= amorphous.length)
alert('Error reading in amorphous database around "'
 + amorphous.substring(pos,pos+30) + '"');
pos = amorphous.indexOf("*",pos) + 1;
 }
 }
 return this;
}
function search(str) {
 menu_length = 0;
 temp = new Object();
 temp_length = 0;
 words_length = 0;
 words = new Object();
 pos = 0;
 while ((pos = str.indexOf(" ")) != -1
 && and_search != "exact") {
 words[words_length] = str.substring(0,pos);
 if (words[words_length].length > 0)
words_length++;
 if (str.length == 1)
str="";
 else
str = str.substring(pos+1,str.length);
 }
 if (str.length > 0)
 words[words_length++] = str;
 for (q=0;q<words_length;q++) {
 temp_length = 0;
 str = words[q].toLowerCase();
 len = (and_search=="and"&&q>0?menu_length:database_length);
 for (n=0; n<len; n++) {
if (and_search=="and"&&q>0) {
combo = (menu[n].title + " " + menu[n].description
 + " " + menu[n].keywords).toLowerCase();
} else {
combo = (database[n].title + " " + database[n].description
 + " " + database[n].keywords).toLowerCase();
}
if (combo.indexOf(str) != -1) // found
temp[temp_length++] = (and_search=="and"&&q>0?menu[n]:database[n]);
 }
 if (and_search!="and" && q>0) {
added = 0;
for (i=0;i<temp_length;i++) {
duplicate = false;
for (j=0;j<menu_length&&!duplicate;j++) {
 if (menu[j] == temp[i]) {
 duplicate = true;
 }
}
if (!duplicate)
 menu[menu_length+(added++)] = temp[i];
}
menu_length += added;
 } else {
for(h=0;h<temp_length;h++)
menu[h] = temp[h];
menu_length = temp_length;
 }
 }
}
function entry() {
 if ((document.entryform.keyword.value.length == 0)
 || (document.entryform.keyword.value == " ")) {
 alert("First you must enter a keyword to search for.");
 return false;
 }
 and_search = (document.entryform.and_or.selectedIndex == 0?"and":"or");
 if (document.entryform.and_or.selectedIndex == 2)
 and_search = "exact";
 location.href = location.pathname + "?"
 + escape(document.entryform.keyword.value)
 + (and_search != "or"?"&"+and_search:"");
 return false;
}
function redWord(str) {
 for(r=0; r<words_length; r++) {
 pos = -3;
 word = words[r].toLowerCase();
 while ((pos = str.toLowerCase().indexOf(word,pos+3)) != -1) {
val = pos+word.length;
str = str.substring(0,pos) + "*"
+ str.substring(pos,val) + "|"
+ str.substring(val,str.length);
 }
 }
 pos = -16;
 while ((pos = str.toLowerCase().indexOf("*",pos+16)) != -1)
 str = str.substring(0,pos) + "<font color=red>"
+ str.substring(pos+1,str.length);
 pos = -7;
 while ((pos = str.toLowerCase().indexOf("|",pos+7)) != -1)
 str = str.substring(0,pos) + "</font>"
+ str.substring(pos+1,str.length);
 return str;
}
var amorphous = document.database.list.value;
temp_str = amorphous.substring(amorphous.length-2,amorphous.length);
if (temp_str.indexOf("*") == -1)
 amorphous += "* ";
else
 amorphous += " "; // amorphous database must have characters after last asterisk
database_length = 0; // Netscape 2 fix
var database = new Database(); // read in from amorphous database
menu_length = 0; // Netscape 2 fix
var menu = new Object();
string = "";
and_search = "or";
if (location.search.length > 1) {
 string = unescape(location.search.substring(1,location.search.length));
 pos = 0;
 while ((pos = string.indexOf('"',pos)) != -1) {
 string = string.substring(0,pos) + '\\"' + string.substring(pos+1,string.length);
 pos += 2;
 }
 if (string.substring(string.length-4,string.length) == "&and") {
 string = string.substring(0,string.length-4);
 and_search = "and";
 } else if (string.substring(string.length-6,string.length) == "&exact") {
 string = string.substring(0,string.length-6);
 and_search = "exact";
 } else if (string.substring(string.length-3,string.length) == "&or") {
 string = string.substring(0,string.length-3);
 and_search = "or";
 }
 search(string);
}
document.write('<form name="entryform" onSubmit="return entry()">'
 +'<b>Search for:</b><br><input type="text" size=20 '
 +'name="keyword" value="'+string+'"> '
 +'<input type="button" value="Search" onClick="entry()"><br><select name="and_or" '
 +'size=1><option'+(and_search=="and"?" selected":"")+'>find all words '
 +'(AND)<option'+(and_search=="or"?" selected":"")+'>find any word '
 +'(OR)<option'+(and_search=="exact"?" selected":"")+'>exact '
 +'match</select></form><br>');
if (location.search.length > 1)
 document.write('<b>Results:</b><br><br>\n');
// eliminate the keywords portion of the statement below to eliminate them from your display
for (n=0; n<menu_length; n++)
 document.write('<b><a href="'+menu[n].url+'">'+menu[n].title
 +'</a></b><br>'+redWord(menu[n].description)+'<br>Keywords: ' 
 +redWord(menu[n].keywords)+'<br><br>\n');
if ((menu_length == 0) && (location.search.length > 1))
 document.write('Keyword "'+string+'" not found!\n');
// -->
</script>
<hr noshade>
<br><br>
The following is just a demonstration of how you would link to search.htm from other pages:<br><br>
<!-- if you want to link from another page, just copy the JavaScript code below
 and paste it into your other page -->
<script language="JavaScript">
<!--
// enter full or relative URL to your search page
var search_htm_url = "your_search_page_name.htm";
function searchPage() {
 if ((document.searchpage.keyword.value.length == 0)
 || (document.searchpage.keyword.value == " ")) {
 alert("First you must enter a keyword to search for.");
 } else {
 sel = document.searchpage.and_or.selectedIndex;
 location.href = search_htm_url + "?"
+ escape(document.searchpage.keyword.value)
+ (sel==0?"&and":(sel==2?"&exact":"&or"));
 }
 return false;
}
document.write('<form name="searchpage" onSubmit="return searchPage()">'
 +'Search for: <input type="text" size=15 name="keyword"> '
 +'<input type="button" value="Search" onClick="searchPage()"><br>'
 +'<select name="and_or" size=1><option>find all words (AND)<option>find any word '
 +'(OR)<option>exact match</select><br>"*" wildcards are valid</form>');
// -->
</script>
<br><br>
Or you can just use direct links. For instance, if you wanted to search for the keyword "javascript", then you would link to <a href="search.htm?javascript">search.htm?javascript</a>. Or search for "javascript" or "perl": <a href="search.htm?javascript%20perl&or">search.htm?javascript%20perl&or</a>. Or for "javascript" and "perl": <a href="search.htm?javascript%20perl&and">search.htm?javascript%20perl&and</a>. Or for the exact phrase "javascript perl": <a href="search.htm?javascript%20perl&exact">search.htm?javascript%20perl&exact</a>. (note that %20 is the URL escaped form of a space).
</body>
</html>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/1/2000 8:35:33 PMThunderSoft Inc.

Please leave comments!
(If this comment was disrespectful, please report it.)

 
5/1/2000 9:00:57 PMAra

coo
(If this comment was disrespectful, please report it.)

 
5/1/2000 11:06:21 PMMichael Wieck

this is an EXCELLENT script. good job!
(If this comment was disrespectful, please report it.)

 
5/10/2000 7:39:47 PMtsi

Please Vote for Me!!!!!!!
(If this comment was disrespectful, please report it.)

 
6/20/2000 12:57:49 PMMichael Meeks

Hi, do you have a working example you could email me ?

//HTML for :A Search Engine using
// JavaScript! No external files
//dependencies

Thanks
(If this comment was disrespectful, please report it.)

 
11/30/2000 1:47:07 PMMarcio

I have tried to run your code, but I am getting a error saying that Menu_Length is not defined.
Can you help.
Thank you in advance,

Marcio
(If this comment was disrespectful, please report it.)

 
11/30/2000 2:06:09 PMMarcio Coelho

Hi,
I just sent you an EMail before asking help for an error for Menu_Length.
Well, That was simply a Case Sensitive issue that I modified.
However, I am unable to search and that is of course because I am not undertanding some of the parameters.
What you mean by key words, and description.
Can you give me some example ?

Thank you so much,

Marcio
(If this comment was disrespectful, please report it.)

 
2/23/2002 7:40:31 PMKenny

Hey, I use this code on my website and the users love it! I modified the code just enough to suit my needs and I posted it! Thanks Kenny
(If this comment was disrespectful, please report it.)

 
5/2/2003 7:44:15 AM

Duh...it won't work...
(If this comment was disrespectful, please report it.)

 
8/22/2006 5:48:47 PMalind khanna

would you mail me working project on web search engine in java my email id alindcool@gmail.com
(If this comment was disrespectful, please report it.)

 
3/13/2007 1:51:15 AMPritam Das

i m facing problem in running the search engine code........it's not running.can u help me out??
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.