Friday, March 13, 2015

Creating a simple search engine using Javascript

Before we start, you must have a little knowledge on functions in JavaScript and some basic HTML tags. If you are still unknown about these things you may copy the example here but you wont be able to modify it for your purpose of use, so please refer to JavaScript and HTML official web site for details in them and get some time to study functions and basic tags.

For those who already carry information on JavaScript and HTML, here we go. Firstly lets us sketch the skeleton for our search user interface.


search.html
[Note: please fill in other validating tags for a complete HTML file]

 <table width ="200" border="0" cellspacing="0">  
 <tr>  
 <td colspan="2">Sarch </td>  
 </tr>  
 <tr>  
 <td>  
 <input type="text" name="query" size="100">  
 </td>  
 <td>  
 <input type="button" name="Search" value="GO">  
 </td>  
 </tr>  
 </table>  



after we are done with html now we go to JavaScript Section, here we first create a function that does search as it is the vital function for our project.

 //For commencing search  
 function doSearch ( s ) {  
 openDbRelativeURL("All?SearchView&Query=" + s.value);  
 }  

Now as we have done our doSearch function

 function doSearch ( s ) {  
 var regExp1 = /\bfield\b/;  
 var regExp2 = /[(,),<,>,\[,\]]/;  
 var str = s.value; if ( str == "" ){  
 alert("Please be sure to enter something to search for.");  
 s.focus();  
 } else {  
 if ( typeof regExp1.source != 'undefined' ) //supports regular expression testing  
 if ( regExp1.test( str ) || regExp2.test( str ) ){  
 var alrt = "Please note that you can not include:";  
 alrt += "\n\nThe reserved word 'field'\nthe characters [, ], (, ), < or >";  
 alrt += "\n\nin your search query!\n\nIf you are confident that you know";  
 alrt += "\nwhat you are doing, then you can\nmanually produce the URL required."  
 s.focus();  
 return alert( alrt );  
 }  
 openDbRelativeURL("All?SearchView&Query=" + escape( str ) + "&start=1&count=10");  
 }  
 } 

after dosearch function the components that are searched should be displayed by finding it so we create another function  openDBRelative URL

 //For opening the related URL  
 function openDbRelativeURL( url, target ){  
 //Check we have a target window;  
 target = (target == null ) ? window : target;  
 //Work out the path of the database;  
 path = location.pathname.split('.nsf')[0] + '.nsf/';  
 target.location.href = path + url;  
 }


Lastly Paste these codes inside <script> </script> tags inside your html file or include them providing link.

[Note: I haven't checked this example in m local machine or live server.... might work please let me informed if something goes wrong]





No comments:

Post a Comment