home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Mode Examples / JavaScript-Example.js < prev    next >
Encoding:
Text File  |  2000-10-30  |  2.2 KB  |  91 lines

  1. // Java-Example.java
  2. // 
  3. // Included in the Alpha distribution as an example of the JScr mode.
  4. // 
  5. // Source of original document:
  6. // 
  7. // http://www.npac.syr.edu/projects/tutorials/JavaScript/examples/
  8.  
  9. <HTML>
  10.  
  11. <HEAD>
  12. <TITLE>JavaScript Example:  Array Sort</TITLE>
  13.  
  14. <SCRIPT LANGUAGE="JavaScript">
  15. <!-- hide script from old browsers
  16.  
  17. president = new Array(10);
  18. president[0] = "Washington";
  19. president[1] = "Adams";
  20. president[2] = "Jefferson";
  21. president[3] = "Madison";
  22. president[4] = "Monroe";
  23. president[5] = "Adams";
  24. president[6] = "Jackson";
  25. president[7] = "Van Buren";
  26. president[8] = "Harrison";
  27. president[9] = "Tyler";
  28.  
  29. // Returns -1,0,1 if the first string is lexicographically 
  30. // less than, equal to, or greater than the second string,
  31. // respectively:
  32. function compareStrings(a, b) {
  33.   if ( a < b ) return -1;
  34.   if ( a > b ) return 1;
  35.   return 0;
  36. }
  37.  
  38. // Return -1,0,1 if the first string is shorter than, equal to
  39. // (in length), or longer than the second string, respectively:
  40. function compareStringLength(a, b) {
  41.   if ( a.length < b.length ) return -1;
  42.   if ( a.length > b.length ) return 1;
  43.   return 0;
  44. }
  45.  
  46. // Sort and display array:
  47. function sortIt(form, compFunc) {
  48.   var separator = ";";
  49.   if ( compFunc == null ) {
  50.     president.sort();     // lexicographical sort, by default
  51.   } else {
  52.     president.sort(compFunc);  // use comparison function
  53.   }
  54.   // display results
  55.   form.output.value = president.join(separator);
  56. }
  57.  
  58. // end script hiding -->
  59. </SCRIPT>
  60.  
  61. </HEAD>
  62.  
  63. <BODY BGCOLOR="#FFFFFF"
  64.       onLoad="document.forms[0].output.value = president.join(';')">
  65.  
  66. <H2>An array of character strings</H2>
  67.  
  68. Listed below are the first ten Presidents of the United States. <P>
  69.  
  70. <FORM>
  71. <INPUT TYPE="text" NAME="output" SIZE=100> <P>
  72. Click on a button to sort the array: <P>
  73. <INPUT TYPE="button" VALUE="Alphabetical" 
  74.        onClick="sortIt(this.form, compareStrings)">
  75. <INPUT TYPE="button" VALUE="Name Length" 
  76.        onClick="sortIt(this.form, compareStringLength)">
  77. <INPUT TYPE="button" VALUE="Chronological" 
  78.        onClick="self.location.reload()">
  79. </FORM>
  80.  
  81. <P> <HR> Note:
  82. <OL>
  83.   <LI>Alphabetical: uses JavaScript sort() default method on text strings
  84.   <LI>Name Length: uses user-defined comparison function
  85.   <LI>Chronological: reloads original array
  86. </OL>
  87.  
  88. </BODY>
  89.  </HTML>
  90.  
  91.