Display the Greek Alphabet with VBScript

How to Use VBScript to Display Tables of Greek Letters on a Web Page

© Mark Alexander Bain

Jun 26, 2009
Display All of the Greek Letters with VBScript, Mark Alexander Bain
The Greek alphabet cannot typed directly into a web page. Instead ASCII codes must be used. So, intead of having to remember 48 separate codes just use VBScript code.

A web site designer will often wish to use Greek letters on their web pages. For example, they may want to display something like:

Circle Circumference = 2 * π * Radius

or:

a sin θ + b cos θ = A sin ( θ + α)

If that's the case then they'll need to know some ASCII (American Standard Code for Information Interchange) codes, for example:

  • θ = θ
  • α = α

On the other hand, if they don't want to remember these codes (and the other 46 codes for all of the upper and lower case Greek letters) then they can just write some VBScript to display all of the Greek letters in a table.

The Greek Letters

The first step is to create an array containing the names of the Greek letters:

Dim Letters : Letters = Array( _
  • "Alpha","Beta","Gamma","Delta", "Epsilon", "Zeta", _
  • "Eta", "Theta", "Iota", "Kappa", "Lambda", "Mu", _
  • "Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma", _
  • "Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega")

The ASCII codes for the letters lie in two ranges:

  • 913 to 936 = upper case Greek letters (from Α to Ψ)
  • 945 to 968 = lower case Greek letters (from α to ψ)

And so with this knowledge, and the array of letter names, the developer can create the operating parameters of the VBScript functionality:

Dim Ascii_start : Ascii_start = 913 ' Range starts at 913
Dim Ascii_end : Ascii_end = Ascii_start + ubound(Letters) 'Range ends at 936
Dim Ascii_space: Ascii_space = 32 'Lower case range is 32 higher than upper case

The programmer can now uses this information to create the required subroutines and functions.

A VBScript Subroutine for Displaying all of the Greek Letters in a Table

It's interesting to view all of the Greek letters in a table on a web page, and a programmer can achieve this easily with VBScript. The programmer must, therefore, add a suitably named subroutine and the local variables that it will require:

Sub all_greek_letters
Dim L0 'Letter number
Dim L1 'Upper Case range start
Dim L2 'Lower Case range start

Next the table headings can be defined:

document.write ("<table width=100%>")
document.write ("<th>Name")
document.write ("<th>HTML Code<th>Upper Case Greek Letter")
document.write ("<th>HTML Code<th>Lower Case Greek Letter")

The code for displaying the Greek letters is quite simple. It:

  • steps through the array of letters
  • uses the current position to calculate the correct ASCII code for the upper case letter
  • uses the ASCII code for the upper case letter to calculate the correct ASCII code for the lower case letter

And this code will be something like:

For L1 = Ascii_start to Ascii_end
L0 = L1 - Ascii_start L2 = L1 + Ascii_space
document.write ("<tr>")
document.write ("<td align=center>" & Letters(L0) & "</td>")
document.write ("<td align=center>&#" & L1 & "</td>")
document.write ("<td align=center>&#" & L1 & "</td>")
document.write ("<td align=center>&#" & L2 & "</td>")
document.write ("<td align=center>&#" & L2 & "</td>")
document.write ("</tr>")
Next
document.write ("</table>")
End Sub

The subroutine itself is called by adding the code:

all_greek_letters

If this VBScript is added to a web page then a table containing all of the Greek letters (in both upper and lower case) and their ASCII codes will be displaying (as shown in figure 1 at the bottom of this article). Of course, any programmer's should now be thinking about how to convert the above code, so that they can display α to ψ as individual letters rather than all of the letters at the same time.


The copyright of the article Display the Greek Alphabet with VBScript in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish Display the Greek Alphabet with VBScript in print or online must be granted by the author in writing.


Display All of the Greek Letters with VBScript, Mark Alexander Bain
Figure 1: A Table of Greek Letters, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo