Contact Us | Search |
Java Script Calculator
How Does This Sample Work?
The calculator is an HTML table containing button
controls. The buttons themselves are intrinsic
<INPUT> style buttons, not Microsoft«
ActiveX« controls. Microsoft« JScript« code is
attached to the OnClick event for these buttons.
The display is simply an intrinsic <INPUT>
style text box.
Notice the variable declared at the beginning of
the <SCRIPT> tag, outside of any procedure:
// Module-level variables
var FKeyPad = document.Keypad;
var Accum = 0; // Previous number (operand)
var FlagNewNum = false; // Flag to indicate a new
number (operand) is being entered
var PendingOp = ""; // Pending operation
waiting for completion of second operand
. . . view source for the rest ▄
These are module-level variables that persist for
as long as the page is loaded. By contrast,
variables declared within a procedure cease to
exist as soon as the procedure exits. Thus,
module-level variables can be used to store values
that must persist between invocations of a
procedure. Module-level variables are also visible
to all procedures, and so are useful when multiple
procedures need to share values.