Hard Reload
To See Menu |
|
|
<script
language="JavaScript">
<!--
elim = ""
LookFor = ""
LookBy = ""
k = 0
g = 0
n = 0
var FoundArray = new Array(k)
|
Initialize all global variables. This allows
any function to access, their last known values. elim,
LookFor, & LookBy are string variables. k,
g, & n are numbers.
The array FoundArray is also initialized, it
will be used to build an array of search results, on the fly. |
|
|
function
listing(first,last,dept,phone,ext,email) {
this.first = first;
this.last = last;
this.dept = dept;
this.phone = phone;
this.ext = ext;
this.email = email; }
|
The
function listing,
sets up the columnar headings of the 2D array directory which follows.
The properties first,last,dept,phone,ext,email, correspond to a similarly named text object. |
|
|
directory
= new Array(6)
directory[0] = new listing
("bob","johnson","accounting","(206)
555-5555","3704","jbob@company.com")directory[1] = new listing
("carrol","ethridge","operations","(206)
555-5555","3712","carrole@company.com")
directory[2] = new
listing
("bob","allen","accounting","(206)
555-5505","6304","bob@company.com")
directory[3] = new
listing
("henry","murphy","sales","(621)
555-5855","9004","murph@company.com")
directory[4] = new
listing
("susan","jones","shipping","(621)
555-5855","6774","jones@company.com")
directory[5] = new
listing
("susan","murphy","accounting","(621)
555-5855","6774","murphy@company.com")
|
The array directory contains six
entries [0]-[6].
Each listing
contains the the six properties first,last,dept,phone,ext,email.
The script will search each listing by the property that corresponds to the users search input. For example,
if the user inputs Sales into the form dept field, the script will search the listing property dept.
All of the listing properties are entered as lowercase strings. This is to eliminate the
need for case conversion of the listing.property, when the search is performed.
Instead, the LookFor string used later in the script is converted toLowerCase(), to match the
convention used in the array.
|
|
|
function LookNow(form) {
var elim = ""
n = 0
k = 0
j = 0
for ( var j = 0 ; j < (document.forms[0].elements.length) - 4 ; j++ ){
elim = document.forms[0].elements[j].value;
if ( elim == "" || null) { continue; }
else {
LookFor = document.forms[0].elements[j].value.toLowerCase();
LookBy = document.forms[0].elements[j].name;
SearchNow(LookFor,LookBy);
break; }}}
|
LookNow searches the forms
fields using the document object's forms elements array. forms[0].elements[j]. If the field is "" || null, the
element is skipped using continue;.
If a value is found, the script assigns that value to LookFor, notes the field that it came from as LookBy, sends them both to SearchNow, & finally, break's the loop.
Some of the global variables are reset here, to enable a fresh search. To keep the results
of the last search available, n & k would be left out here and added to an onReset="n=0;k=0" event handler to the form tag.
N
O
T
E |
The for loop in LookNow, searches the
(document.forms[0].elements.length)
- 4. The - 4 is to prevent the function from attempting to search the
form button, submit and reset objects. |
|
|
|
SearchNow(LookFor,LookBy)
{
i = 0
for ( var i = 0 ; i < directory.length ; i++ ) {
if ( LookFor != eval("directory[" + i + "]." + LookBy ))
{ continue; }
{k++; OutPutPrep(i,k); }
alert("Your search for " + elim + ",\nyielded " + k + "
matches") }
|
SearchNow
takes the values passed to it by LookNow: LookFor & LookBy
and uses them to compare the appropriate listing
property in the directory array.
If a match is found, i records the row it
was found in, increments k++ and sends them
both to OutPutPrep, then continues searching
for additional matches.
N
O
T
E |
The eval() string method is used to convert the string "directory[" + i + "]." + LookBy to an actual object. This is done because LookBy is a string. For example, eval()
may return directory[i].first or directory[i].ext, etc. |
|
|
|
function
OutPutPrep(i,k) {
n = 0
for ( n ; n < k ; n++ ) {
FoundArray[n] = "directory[" + i + "]"; OutPutResult(0); continue;}
|
OutPutPrep
populates the array FoundArray, that was
declared earlier. FoundArray uses k to determine its length.
Since each i passed to OutPutPrep,
also has a paired k++, k
keeps track of how many i's have passed
through OutPutPrep's gate. n increments for each i,k
pair, generating FoundArray's rows on
the fly.
|
|
|
function
update(g) { return 1; }
|
update(g)
keeps track of which row in the FoundArray
array is being viewed.
|
|
|
function
NextName() {
update(g++);
if ( g > k-1 ) { g = k-k; } OutPutResult(g);}
|
NextName(),
first reports it's destination to update(g).
The internal reset if ( g > k-1 ),
ensures that g does not step beyond the
relative bounds of FoundArray.
|
|
|
function
PrevName() {
update(g--);
if ( g < 0 ) { g = k-1; } OutPutResult(g);}
|
PrevName(),
first reports it's destination to update(g).
The internal reset if ( g < 0 ), ensures
that g does not step beyond the relative
bounds of FoundArray. |
|
|
function
OutPutResult() {
if ( k != 0 ) {
document.forms[0].elements[0].value = eval (FoundArray[g] + ".first"); document.forms[0].elements[1].value
= eval (FoundArray[g] + ".last");
document.forms[0].elements[2].value = eval (FoundArray[g] + ".dept");
document.forms[0].elements[3].value = eval (FoundArray[g] + ".phone");
document.forms[0].elements[4].value = eval (FoundArray[g] + ".ext");
document.forms[0].elements[5].value = eval (FoundArray[g] + ".email"); }}}
//-->
</script> |
OutPutResult
first checks to make sure that FoundArray is
not empty by testing if ( k != 0 ).This
prevents eager users from tripping an error by clicking on the navigation buttons Prev & Next before
searching the array.
simply writes the search results to the form.
|
|
|
|