Search Site Form

[Total: 0    Average: 0/5]

This example has a search method to it.

Body Code:

<script language="JavaScript"> 
<!-- 
var LOC         = 0;
var TITLE       = 1;
var KEYWORDS    = 2;
var DESCRIPTION = 3;
var database = new Array();
function addEntry(_l,_t,_k,_d) {
  var _next = database.length?database.length:0;
  database[_next] = new Array();
  database[_next][LOC]=_l;
  database[_next][TITLE]=_t;
  database[_next][KEYWORDS]=_k;
  database[_next][DESCRIPTION]=_d;
}
addEntry("http://www.js-examples.com/","Your First Page Title", "keywords for first file go here separated by spaces", "First page description here!");
addEntry("http://www.js-examples.com/","Your Second Page Title","keywords for second file go here separated by spaces","Second page description here!");
addEntry("http://www.js-examples.com/","Your Third Page Title", "keywords for third file go here separated by spaces", "Third page description here!");
addEntry("http://www.js-examples.com/","Your Fourth Page Title","keywords for Fourth file go here separated by spaces","Fourth page description here!");

var menu = new Array();
function addMenu(_i) {
  //alert(_i);
  var _next = menu.length?menu.length:0;
  menu[_next] = new Array();
  menu[_next][LOC]=database[_i][LOC];
  menu[_next][TITLE]=database[_i][TITLE];
  menu[_next][KEYWORDS]=database[_i][KEYWORDS];
  menu[_next][DESCRIPTION]=database[_i][DESCRIPTION];
}
// this function takes these arguments
// 1> pattern to search for - deliminated by spaces.
// 2> true for OR
// 3> true for AND
// 4> true for EXACT
// return value is the index found or -1 if none found
function findThis(_searchfor,_or,_and,_exact) {
  var S = _searchfor.split(" ");
  for(var i=0;i<database.length;i++) {
    if(_or) {
  for (var j=0;j<S.length;j++) {
    if (database[i][KEYWORDS].indexOf(S[j])     != -1 ||
    database[i][DESCRIPTION].indexOf(S[j])  != -1 ||
database[i][TITLE].indexOf(S[j])        != -1 ||
database[i][LOC].indexOf(S[j])          != -1) {
  addMenu(i);
}
  }
} else if(_and) {
  // AND
  var count=0;
  for (var j=0;j<S.length;j++) {
    if (database[i][KEYWORDS].indexOf(S[j])     != -1 ||
    database[i][DESCRIPTION].indexOf(S[j])  != -1 ||
database[i][TITLE].indexOf(S[j])        != -1 ||
database[i][LOC].indexOf(S[j])          != -1) {
      count++;
}
  }
  //alert(count+"n"+S.length);
  if (count==S.length) {
    addMenu (i);
  }
} else if (_exact) {
  //alert("["+_searchfor+"]n["+database[i][DESCRIPTION]+"]")
  if (database[i][KEYWORDS].indexOf(_searchfor)     != -1 ||
  database[i][DESCRIPTION].indexOf(_searchfor)  != -1 ||
      database[i][TITLE].indexOf(_searchfor)        != -1 ||
      database[i][LOC].indexOf(_searchfor)          != -1) {
addMenu(i);
  }
}
  }
  return (-1);
}


// this function ENTRY
// is called when the new search is requested.
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; 
}

var string="";
var and_search="";
function parseParms() {
  var _l1 = document.location.toString().split("?");
  var _l2 = "";
  var _l3 = "";
  if (_l1.length>=1)
    _l2 = _l1[1];
  if (_l2 && _l2.length>0) {
    if (_l2.indexOf("&") != -1) {
  _l3 = _l2.split("&")[1];
  _l2 = _l2.split("&")[0];
}
  }
  string = unescape(_l2 || "");
  and_search= unescape(_l3 || "or");
}
parseParms();
if (string && string != "") findThis(string,and_search=="or",and_search=="and",and_search=="exact");

var _a  = '';
    _a += '<form name="entryform" onSubmit="return entry()">';
_a += '<b>Search for:</b><br>';
_a += '<input type="text" size=20 name="keyword" value="'+string+'"> ';
_a += '<input type="button" value="Search" onClick="entry()"><br>';
_a += '<select name="and_or" size=1>';
_a += '<option'+(and_search=="and"?" selected":"")+'>find all words (AND)';
    _a += '<option'+(and_search=="or"?" selected":"")+'>find any word (OR)';
_a += '<option'+(and_search=="exact"?" selected":"")+'>exact match';
_a += '</select>';
_a += '</form><br>';
document.write(_a);
if (location.search.length > 1) 
document.write('<b>Results:</b><br><br>n'); 

for (n=0; n<menu.length; n++) {
  var _a  = '';
      _a += '<b><a href="'+menu[n][LOC]+'">'+menu[n][TITLE]+'</a></b><BR>';
      _a += menu[n][DESCRIPTION]+'<br>';
  _a += 'Keywords: '+menu[n][KEYWORDS]+'<br><br>';
  document.write(_a);
}
if ((menu.length == 0) && (location.search.length > 1)) 
  document.write('Keyword "'+string+'" not found!n'); 
// --> 

</script>

Times Viewed: 7

Leave a Reply

Your email address will not be published. Required fields are marked *