March 16, 2011

0 Create a JavaScript Search Engine

Creating a JavaScript Search Engine, so far as JavaScript is concerned, is actually quite easy. The following is a search for this whole site.

Try searching some words. And try some it most likely won't find to see what happens. Of course, you could try 'script' and 'javascript'. 


Javascript search engine









<SCRIPT LANGUAGE="JAVASCRIPT">

<!--//

function MyFunction(){

for (i=0;i<document.F187.MySelect.options.length; i++){

document.F187.MySelect.options[i]=new Option("","")

}

/* First we zero off the options, so we get rid of previous searches, if any. */

/* ####################### Only the data below needs to be changed ####################### */

URLs=new Array()

Titles=new Array()

Keywords=new Array()

URLs[0]="contact.htm";

Titles[0]=&quot;FURTHER INFORMATION ABOUT WEB SITE CREATION.";

Keywords[0]="CONTACT,FEEDBACK,REQUEST FURTHER INFORMATION,";

/* ..... There are a lot of URLs, Keywords, Titles so I have just included the first set for one page */

var joined=new Array()

/* We start off a new array by joining together the titles and keywords */

{

for (i=0;i<URLs.length; i++){

joined[i]=Titles[i]+Keywords[i]

}

Searchphrase=document.F187.MyText.value

/* We get the Searchphrase from the form which the user has filled in */

j=0;

checker=0;

if (document.F187.MyText.value.length<1){

alert('Please enter something to search for.');

/* Or if the user hasn't entered anything at all, we tell them off! And tell them to put something in if they want us to search. */

}

else {

for (i=0;i<URLs.length; i++)

{

if (joined[i].indexOf(Searchphrase.toUpperCase())>-1){

/* We search through our list of URLs and words to see if the search phrase is there. We have already put all our keywords in in upper case, and we make the user's search phrase upper case. */

checker=1;

/* We have found a match! */

document.F187.MySelect.options[j+1]=new Option(Titles[i],URLs[i]); j++

/* We put the found data in the drop down menu in the second position because we want to put a title in the first position, saying whether anything has been found or not. */

}

if (checker==1){

document.F187.MySelect.options[0]=new Option('Results:','')

/* If something is found, then we call the list results. Otherwise we aplogise for not being able to find the thing searched for. */

}

else {

document.F187.MySelect.options[0]=new Option('Sorry! Couldn\'t find anything:','')

}

}

}

}}

//-->

</SCRIPT>

<SCRIPT LANGUAGE="JAVASCRIPT">

<!--//

function go(){

/* When our user presses the button (in the form) we check to see if anything has been selected. Also we don't want the first selection which is 'Results' or the sorry message. */

if (document.F187.MySelect.selectedIndex>0){

location=document.F187.MySelect.options[document.F187.MySelect.selectedIndex].value

}

else {

alert('Please select an option.')

}

}

//-->

</SCRIPT>

The search contains 218 URLs with their data! So it is rather bit long. Though the JavaScript to run it is rather simple. Its code is as follows:



    <form NAME="F187" METHOD="POST" ACTION ENCTYPE="TEXT/PLAIN">

    <p><br>

    Enter a search word or phrase:<br>

    <input TYPE="TEXT" NAME="MyText" VALUE SIZE="27" MAXLENGTH="25"><br>

    <select NAME=&quot;MySelect" size="5">

    <option>Your answer will appear here </option>

    </select> <input TYPE="BUTTON" VALUE="Search" onClick="MyFunction()"><br>

    <input TYPE="BUTTON" VALUE="Go!" onClick="go()" }><br>

    </p>

    </form>



Credit & Big Thanks to KEN WARD for this

0 comments:

Post a Comment

Blogger Themes

 
Powered by Blogger