Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I have a search engine on my site??

Javascript Search Engine

How can I have a search engine on my site??

by  GUJUm0deL  Posted    (Edited  )
Hi, everyone...ok, i've had a problem with this for a long time...I had wanted a site search engine for my site (http://www.geocities.com/GUJUm0deL) but didn't like using ANTZ and other softwares out there...and geocities didn't support Coldfusion or any other databases, so one day surfing the net I came across a script, which was all messed and didn't even work right, but the syntax and concept seemed logical, so I used my Javascript know-how and made it work...and here it is!! Just copy and paste the code the way you see it here onto a fresh HTML page (you can add the content later on).


Just copy and paste the code the way you see it here onto a fresh HTML page (you can add the content later on)

[color red]
Code:
<html><HEAD>
<meta name="AUTHOR" content="GUJUM0deL (ketan mehta)">
<meta name="description" content="search engine using JS">

<SCRIPT LANGUAGE="JavaScript">
/*INSTRUCTIONS:
  Ok, this is the setup and its quite simple to figure out if you know JS or any other type
of scripting language.  First off in the ARRAYs you write in the HTML page, the path of the
site (which can be left blank), a description to what the link is, the keywords entered for
the search criteria, and finaly the description to what the link is about.  (IF CONFUSED JUST RUN THE SCRIPT THE WAY IT IS FIRST AND SEE THE RESULTS! OR, JUST EMAIL ME BACK AND I'LL EXPLAIN)
*/
var item = new Array();
Km=0; item[Km]=new Array("hotmail.com","http://www.","Hotmail","all, any, free, email, e-mail","FREE E-Mails!!");
Km++; item[Km]=new Array("Yahoo.com","http://www.","Yahoo","all, any, yahoo","Wanna Yahoo!");
Km++; item[Km]=new Array("tvguide.com","http://www.","TVGuide.com","all, any, tv, guide, shows","TV Guide");

//YOU CAN EDIT WHAT THE NEW WINDOW WILL LOOK LIKE, SUCH AS TEXT COLOR, BACKGROUND COLOR, LINK COLOR.
pageview="<html><head><title>Search Results</title></head><body bgcolor='black' text='white' link='#bda940' vlink='#cccccc'><left><table border=0 width=200>";

function search(formfld) {
//YOU CAN EDIT THE WINDOW PROPERTIES ONLY, NOTHING ELSE.
  windw = window.open("","","toolbar=no,location=no,directories=no,status=no,menubar=no,height=215,width=250, scrollbars=yes,resizable=no");
//DO NOT EDIT FROM HERE!!
  windw.document.write(pageview);
  txt = formfld.searchvalue.value.split(" ");
  findingval = new Array(); total=0;
    for (i = 0; i < item.length; i++) {
      findingval[i] = 0; order = new Array(0, 4, 2, 3);
      for (j = 0; j < order.length; j++)
      for (k = 0; k < txt.length; k++)
      if (item[i][order[j]].indexOf(txt[k].toLowerCase()) > -1 && txt[k] != "")
      findingval[i] += (j+1);
    }
  for (i = 0; i < findingval.length; i++) {
    n = 0; w = -1;
    for (j = 0;j < findingval.length; j++)
    if (findingval[j] > n) { n = findingval[j]; w = j; };
    if (w > -1) total += show(w, windw, n);
    findingval[w] = 0;
  }
  windw.document.write("</table><br>Total found: "+total+"<br>");
  windw.document.write("<INPUT onfocus='blur();' type='button' style='background-color:#004080; border-color:#004080; color:#FFFFFF;' value='Close' onClick='self.close()'");
//PLEASE LEAVE THIS PORTION HERE.  IT IS MY AUTHOR'S TAG.
  windw.document.write("<br><br><font size='1'><center>¬ 2001 by <A HREF='http://www.GEOCITIES.COM/GUJUm0deL' target='_blank'>GUJUm0deL</A>. All right reserved. </center></font> </body></html>");

}
/* This function is important.  Here you actualy show what the search result window display.
DO NOT EDIT HERE.  ONLY EDIT IF YOU KNOW WHAT YOU ARE DOING.
*/
function show(which,dniw,num) {
  link = item[which][1] + item[which][0];
//If you want to you can change the 'target' to anything.
  line = "<tr><td><a href="+link+" target='_new'>"+item[which][2]+"</a><br>";
  line += item[which][4] + "<br><br></td></tr>";
/* By adding this tag: <br>"+link+" between the: + " on the above line you will display the
link in text well.
*/
  dniw.document.write(line);
  return true;
}
</script>
</HEAD>

<BODY bgcolor="#000000" text="bda94c">
<center>
<!--- leave the 'form method' line the way it is.  ALL OF IT -->
<form method=get action="javascript:void(0)" onsubmit="search(this); return false;">
<tr><td>
        <p><b><font color="ff0000">to see it in action type in:</font> </b></p>
        <p>'all'  to see all the search results. <i>Without the single quotes.</i><br>
          'hotmail' to see the search result for that key word.<br>
          You can also enter part of a word and the result will bring back the 
          entry. <i>Try entering: 'ail' and see what happens.</i><br>
        </p>
        <p> This script also has the <b>.toLowerCase()</b> so that it convert 
          all the search words to lowercase. In other words, this script is NOT 
          case sensitive!! áSo if you enter in HOTMAIL or hotMAil or hotMAIl, 
          you still get the right result.</p>
        <p><i> Just makes sure that all the keywords are in lowercase in the Array 
          sction at the top...</i></p>
        <p>NS and IE display it differently, <i>BUT IT WORKS IN BOTH BROWSERS!! 
          á</i>For a cooler effect use CSS for the textbox and buttons, like 
          I did. (áJust a note that NS4 will not display the CSS tags)</p>
        <p><b>PLEASE LEAVE MY AUTHORSHIP IN THE SCRIPT THANKS.</b></p>
        <p>
          <input type=text name=searchvalue value="">
          <input onFocus='blur();' type=submit value="Search" style="background-color:#004080; border-color:#004080; color:#FFFFFF;" name="Input">
        </p>
      </td>
          </tr>
</form>
</center>
</body>
</html>
[/color]

*Rememeber to leave the Km++; item[Km]=new Array declarations all on one line...and also make sure that the windw.document.write(.....) are all on one line, unless you concatenated them...
If for some reason, the code gives you some trouble you can download a working one at my site, under the 'JavaScript/Java' section of 'Downloads'...or simply, type in the word 'search' in the textbox and hit the button...:)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top