The Doc Dialer: Displaying Names | WebReference

The Doc Dialer: Displaying Names


The Doc Dialer

Displaying Names

When we want to print the names found in the trie, we go over the printList array and collect the array elements that are turned on in the array:


function printDisplay() {
  var str = "";
  for (i=0; i<=EMPMAX; i++) {
    if (printList[i] == true) {
      var fileName = extractFileName(empList[i]).toLowerCase();
	      str += '<A HREF=' + '"' + fileName + '.html"' + '>' + empList[i]
	      + '</A>' + '  ' +  '<A HREF="calling.html" >' + empPhone[i] +
        '</A>' + '<BR>';
	  }
  }
  outputStringToDisplay(str);
}

Notice what is printed out. The president's name is given in empList[i]. We print out a link to its bio, stored in a file which is named after the president. Bill Clinton's bio, for example, is stored in billclinton.html. We also print each president's four-digit index number, empPhone[i]. This number is presented as a link to a common file called calling.html. A production application may hook a phone calling here or an access to a database, or any other service that may add another value to the system. We extract the first-link file name in the extractFileName() function:

function extractFileName(empName) {
  if (nameCode == 1 && versionCode >= 5.5) {
    var regExp = /(\w+)\s*(\w+)/g;
    return empName.replace(regExp, matchingFunction);
  }
  else {
    blankPos = empName.indexOf(" ");
    firstName = empName.substr(0, blankPos);
    lastName = empName.substr(blankPos + 1, empName.length - blankPos);
    return firstName + lastName;
  }
}

The top portion of the function above demonstrates a new functionality of Internet Explorer 5.5 and will be explained later in this column. The bottom portion uses a more conventional methods to assemble a file name from the first and last name of a president name. We assume there is only one blank between the first and last name in empName. We first find the position of the blank:

blankPos = empName.indexOf(" ");

The first name starts at position 0 and ends at the blank position:

firstName = empName.substr(0, blankPos);

The last name starts just after the blank and end at the end of empName:

lastName = empName.substr(blankPos + 1, empName.length - blankPos);

Finally, we return the concatenation of the first and last names:

return firstName + lastName;

Once we assemble the string str, we write it to the foundSoFar SPAN element with the outputStringToDisplay() function:

function outputStringToDisplay(str) {
  if (nameCode == 1) {
    document.all.foundSoFar.innerHTML = str;
  }
  else {
    document.foundSoFar.document.write(str);
  }
}

In the case of Internet Explorer (nameCode is 1), we set the innerHTML property of the SPAN element:

document.all.foundSoFar.innerHTML = str;

In the case of Netscape Navigator, we write it to the document object of the SPAN element:

document.foundSoFar.document.write(str);

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: February 14, 2000
Revised: February 14, 2000

URL: http://www.webreference.com/js/column57/8.html