Writing the HTML
As our basic calculator, we'll use the following table:
HTML code for our basic calculator
|
<div align="center">
<table width="30%" border="4">
<tr><td colspan="2" align="center">Display Area</td></tr>
<tr><td>Numeric Buttons</td>
<td>Function Buttons</td>
</tr></table></div>
|
Which implements the layout we discussed in "Planning"
above. We need to put all of the above inside
a <FORM> .. </FORM> pair of tags, and then we need to
create buttons for each of the numbers and functions we talked about
when we planned things.
How do we create buttons? Well, we could do what we did on the
first page of our JavaScript tutorial, and hijack a <input type="reset">
button, but there is a better way. All recent and respectable variants of
JavaScript (but not, for example, some earlier releases of IE 3) have
the following type of input:
<input type="button">
Which gives us a general-purpose button. To each of these buttons we'll
attach a:
OnClick="DoSomething()"
'event handler', so that when we click on the button, we can expect
a response. We'll discuss each of these responses in the
Calculator Functions section below. For now, let's
draw our calculator:
More detailed HTML for basic calculator
|
<FORM name="Calculator">
<table border="4"> <!--OUTER BORDER OF CALCULATOR, PANELS-->
<tr> <!--TOP PANEL------>
<td colspan="2">A Simple JavaScript Calculator<br>
<input type="text" maxlength="40" size="30" name="Display">
</td>
</tr> <!--END TOP PANEL--->
<tr>
<td> <!--LEFT PANEL------>
<table>
<tr>
<td><input type="button" value="7"></td>
<td><input type="button" value="8"></td>
<td><input type="button" value="9"></td>
</tr>
<tr>
{all other rows of digits are similar..}
</tr>
</table>
</td><!--END LEFT PANEL-->
<td> <!--RIGHT PANEL----->
<table>
{right panel is similar in overall structure to left}
</table>
</td><!--END RIGHT PANEL->
</tr></table>
</FORM>
|
We've colour-coded things to make them a bit easier. FORM
elements are in red, the outer table is bold, and
tables inside tables are in blue. You can see
that we've preserved our basic structure that we talked about previously -
the top panel takes up two columns and contains a text input that we'll
use for displaying results; the bottom left panel will contain the numbers
(three examples are given), and the bottom right will contain function
buttons. Note several other things:
- The form has a name. We've called it Calculator.
- Each input button has a value - this is what will be displayed
on the button, for example "7" will be displayed on the button that
you push to generate a "7" on the display.
- The display area is unsurprisingly named Display (with a capital D),
and we've defined a width (size="30") and a maximum length of the
input string (maxlength="40").
- Each of the buttons has its own <td> .. </td> - this
keeps them nicely spaced out and regular.
Now if you take the above code and simply paste it into your web-page,
(adding in a few <td>'s and stuff),
you'll get something like the following:
.. So clearly we need to do a bit of work. We need to:
- Pretty up fonts and alignment
- Widen the buttons a lot
- Perhaps give the calculator a background colour
If you're adequate at HTML, all of this should be reasonably straightforward.
Perhaps widening the buttons needs a bit of explanation. We've given our
buttons values of "7", "8" and so on. What happens if we say:
<input type="button" value=" 7 " >
This is a good way of widening our buttons. Note that even when we
do this, all of the buttons are not quite the same width (at least, on
most browsers)! This is because the chances are your browser is using
a proportional font to write the "value" on the button. Now in Netscape,
the font can be changed for each button to a fixed-width font (although the resulting code
looks like hell) - in IE, you don't seem to have that option (I wonder,
can you do this using style sheets?). Oh well.
Hmm, just for clarity, why don't we also name our buttons. For
example:
<input type="button" value=" 7 " name="seven" >
Play around a bit. Try the 'finished' calculator using both Netscape
and IE - you'll find that despite your fine-tuning, the darn thing looks
quite different. When you're happy, we're ready to move on to actually
making the thing work!
Calculator Functions- Writing the JavaScript
We know how to attach event handlers to each button. We simply
put:
OnClick="DoEvent()"
where DoEvent is the event handler for the relevant button.
For example, for button number seven, we might say:
<input type="button" name="seven" value=" 7 " OnClick="AddDigit('7')">
where AddDigit is the event handler. Let's now decide what
events we need. Here they are:
- Add a digit to the display
- Put a decimal point on the display (if not there already)
- Put an exponent ('e') on the display
- Change the sign to + if - and minus if plus
- Clear and All Clear (C and AC)
- Perform operations * / + and -
- Calculate (on pressing the = button)
- Hmm, what if a user types something into the tempting display
box. It seems cruel to simply discard this, so we have to enter this
(unless it's rubbish)!
Well. Let's think up names for each of these "event handlers" and
then implement each of them in turn. While we're about it, how are we
going to react when a user types something in the display box? Fortunately,
there's an event handler tailored to this need. It's called OnChange.
Here are our names for the event handlers:
But before we implement these event handlers, we need to sort out a
few more things. First we must define the variables we will use in doing our
calculations. Here they are:
Finally, we need to define our constants. There is only one:
The only thing that needs explanation in the above is that we
used a numeric code for the Operation. Hmm, perhaps this is
a bit over the top. We could just keep the "*" or whatever here as
a string. But let's stick with this for the time being!