CSCI 4230
Software Tools
Spring 2000
Suugested Solution to Homework #7

(1)    For example,

<html>
<!--
       By:      Kwok-Bun Yue
       Date:    March 16,2000
       Program: countQuotes.html
-->

<head>
<title>Counting Biological Quotes</title>
<script language="Javascript">
<!-- Hide scripts from older browsers.
//
//    Show the number of Biological quotes.
//
function countQuotes() {
     var inputString = document.quoteForm.inputArea.value;
     document.quoteForm.resultArea.value = "";
     var qcounts = new Object();
     regex = /(Ani|Pla|Ins)\.\d+\.\d+/gi;
     while (matchedArray = regex.exec(inputString)) {
         if (qcounts[matchedArray[0]]) {
            qcounts[matchedArray[0]]++;
         }
         else {
            qcounts[matchedArray[0]] = 1;
         }
     }
     for (var quote in qcounts) {
         document.quoteForm.resultArea.value += quote + ": " + qcounts[quote] + ".\n";
    }
}
// -->
</script>
</head>

<body bgColor=#CCCCCC>

<h3 style="color:blue; size:+2">Count Biological Quotes</h3>
Type in text in the input box that contains Biological quotes.  A
Biological quote has the format of family.volume.page, where
family is one of "Ani", "Pla" and "Ins".  Volumes and pages are
integers.  A biological quote appears in only one line.  The page
finds the number of occurrence of each quotes.
<p>
<form name="quoteForm">
Input Text:
<br>
<textarea name="inputArea" cols="70" rows="5"></textarea>
<p>
<input type=button name="getQuoteCount" value="CountQuote" onClick="countQuotes()">
<p>
Quote count result:
<p>
<textarea name="resultArea" cols="70" rows="8"></textarea>
</form>
</body>
</html>

(2)    For example,

<body>
<script language="javascript">
<!--
var isPos1 = 1;
var y1 = 100;
var x1 = 100;
var y2 = 200;
var x2 = 200;
var message = "Hello, World!";
 

//   Global variables.
var isNav, isIE;
var coll = "";
var styleObj = "";
if (parseInt(navigator.appVersion) >= 4) {
    if (navigator.appName == "Netscape") {
        isNav = true;
    } else {
       isIE = true;
       coll = "all.";
       styleObj = ".style";
    }
}

function displayMessage() {
   //  create the message object.
   //  Use the Layer tag for Navigator.
   if (isNav) {
      theMessage=new Layer(window.innerWidth)
      theMessage.bgColor="#CCCCFF";
      theMessage.document.write(message);
      theMessage.document.close();
      theMessage.top = y1;
      theMessage.left = x1;
      theMessage.visibility="show";
   }
   if (isIE) {
      document.all.theMessage.style.top=y1;
      document.all.theMessage.style.left=x1;
      document.all.theMessage.style.backgroundColor="#CCCCFF";
      document.all.theMessage.style.visibility="visible";
   }
}

function changePosition() {
   if (isNav) {
       if (isPos1) {
          theMessage.top = y2;
          theMessage.left = x2;
          isPos1 = false;
       }
       else {
          theMessage.top = y1;
          theMessage.left = x1;
          isPos1 = true;
       }
   }
   if (isIE) {
       if (isPos1) {
          document.all.theMessage.style.top = y2;
          document.all.theMessage.style.left = x2;
          isPos1 = false;
       }
       else {
          document.all.theMessage.style.top = y1;
          document.all.theMessage.style.left = x1;
          isPos1 = true;
       }
   }
}

//  use the span tag for IE.
if (document.all) {
   document.write('<span id="theMessage" style="position:absolute;visibility:hidden">'+message+'</span>');
}

window.onload=displayMessage;

// -->
</script>
<form>
   <input type="button" onClick="changePosition()" value="Change Position">
</form>
</body>