Hola,
Ton html :
<html>
<head>
<title>Propix - Calculator</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<meta name="keywords" content="Propix, free javascript calculator, freeware calculator, download calculator, JavaScript calculator, component, applet, source, javascripts">
<meta name="description" content="Fully configurable JavaScript calculator. Free software.">
<meta name="author" content="Pintér Gábor, propix@freemail.hu; http://www.propix.hu">
<link rel="stylesheet" href="/styles/main.css" type="text/css">
<script src="/register/log.js"></script>
<script src="/util/util210.js"></script>
<script src="calculator.js"></script>
<script src="../../register/register.js"></script>
</head>
<body class="BlueTile">
<a name="top"></a>
<table class="Max0">
<tr>
<td style="width:240px; text-align=left">
<div id="calcly" style="position:relative; width:240px">
<script>document.write(calc());</script>
</div>
</td>
</tr>
<script>
// Form script
var f=document.frm;
function setpar(calcwidth, calcheight, calcborderwidth, calcspacing,
calcdispwidth, calcdispheight, calcdispfont, calcdispmargin,
calcbuttonwidth, calcbuttonheight, calcbuttonfont, calcbuttonmargin) {
f.calcwidth.value=calcwidth;
f.calcheight.value=calcheight;
f.calcborderwidth.value=calcborderwidth;
f.calcspacing.value=calcspacing;
f.calcdispheight.value=calcdispheight;
f.calcdispwidth.value=calcdispwidth;
f.calcdispfont.value=calcdispfont;
f.calcdispmargin.value=calcdispmargin;
f.calcbuttonwidth.value=calcbuttonwidth;
f.calcbuttonheight.value=calcbuttonheight;
f.calcbuttonfont.value=calcbuttonfont;
f.calcbuttonmargin.value=calcbuttonmargin;
}
// OnClick calls
function setp(n) {
switch (n) {
case 0: setpar(120, 180, 0, 1,
140, 25, "bold 9pt 'Courier New', Courier, mono", 3,
16, 16, "bold 9pt Arial, Helvetica, sans-serif", 3);
break;
case 1: setpar(90, 100, 1, 1,
85, 20, "8pt 'Courier New', Courier, mono", 1,
18, 18, "7pt Arial, Helvetica, sans-serif", 0);
break;
case 2: setpar(150, 200, 3, 0,
150, 50, "bold 24pt 'Courier New', Courier, mono", 5,
35, 35, "bold 14pt Arial, Helvetica, sans-serif", 0);
break;
case 3: setpar(170, 260, 15, 2,
160, 45, "bold 20pt 'Courier New', Courier, mono", 5,
30, 30, "bold 10pt Arial, Helvetica, sans-serif", 4);
break;
case 4: setpar(240, 290, 0, 0,
240, 50, "bold 24pt 'Courier New', Courier, mono", 5,
50, 50, "bold 18pt Arial, Helvetica, sans-serif", 5);
break;
case 5: setpar(240, 290, 4, 0,
240, 50, "bold 30px, Times, serif", 5,
50, 50, "30px, Times, serif", 5);
break;
}
}
// Show default values at startup
setp(0);
// Reload
function update() {
calcwidth=parseInt(f.calcwidth.value);
calcheight=parseInt(f.calcheight.value);
calcborderwidth=parseInt(f.calcborderwidth.value);
calcspacing=parseInt(f.calcspacing.value);
calcdispwidth=parseInt(f.calcdispwidth.value);
calcdispheight=parseInt(f.calcdispheight.value);
calcdispfont=f.calcdispfont.value;
calcdispmargin=parseInt(f.calcdispmargin.value);
calcbuttonwidth=parseInt(f.calcbuttonwidth.value);
calcbuttonheight=parseInt(f.calcbuttonheight.value);
calcbuttonfont=f.calcbuttonfont.value;
calcbuttonmargin=parseInt(f.calcbuttonmargin.value);
setlyHTML(findobj('calcly'), calc());
}
</script>
</body>
</html>
le fichier javascript à placer dans le même dossier :
// Parameters
var calcwidth=220;
var calcheight=240;
var calcborderwidth=1;
var calcspacing=2;
var calcdispheight=50;
var calcdispwidth=210;
var calcdispfont="bold 24pt 'Courier New', Courier, mono";
var calcdispmargin=5;
var calcbuttonheight=35;
var calcbuttonwidth=35;
var calcbuttonfont="bold 14pt Arial, Helvetica, sans-serif";
var calcbuttonmargin=5;
// Generator functions
// Display
function calcdisp() {
return(
"<td colspan=4 align='center' valign='middle'"+
"width='"+calcwidth+"' "+
"height='"+calcdispheight+"' "+
"style='text-align:center; vertical-align:middle;"+
"width:"+calcwidth+"; "+
"height:"+calcdispheight+"'>"+
"<input name='display' type='Text' value='0' "+
"style=\"font: "+calcdispfont+"; "+
"text-align: right; "+
"height: "+(calcdispheight-2*calcdispmargin)+"px; "+
"width: "+(calcdispwidth-2*calcdispmargin)+"px;\""+
"</td>"
);
}
// One button
function calcbutton(value, action) {
return(
"<td align='center' valign='middle' "+
"width='"+(calcwidth/4)+"' "+
"height='"+((calcheight-calcdispheight)/4)+"' "+
"style='text-align:center; vertical-align:middle;"+
"width:"+(calcbuttonwidth+2*calcbuttonmargin)+"; "+
"height:"+(calcbuttonheight+2*calcbuttonmargin)+"'>"+
"<input type='Button' "+
"value='"+value+"' "+
"onClick=\""+action+"\" "+
"style=\"font: "+calcbuttonfont+"; "+
"margin: 0px 0px; "+
"height: "+calcbuttonheight+"px; width: "+calcbuttonwidth+"px\">"+
"</td>"
);
}
// tr
function calctr(s) {
return("<tr>"+s+"</tr>");
}
// Table+form
function calctable(s) {
return(
"<form name='calckeypad'>"+
"<table border='"+calcborderwidth+"' align='center' valign='middle' cellpadding=0 cellspacing='"+calcspacing+"' "+
"width='"+(calcwidth+2*calcborderwidth)+"' "+
"height='"+(calcheight+2*calcborderwidth)+"' "+
"style='text-align=center; vertical-align:middle; "+
"border-spacing:"+calcspacing+"; "+
"width:"+(calcwidth+2*calcborderwidth)+"; "+
"height:"+(calcheight+2*calcborderwidth)+"'"+
">"+
s+
"</table>"+
"</form>"
);
}
// Create calculator
function calc() {
return(calctable(
calctr(calcdisp())+
calctr(
calcbutton("C", "calcclear()")+
calcbutton("CE", "calcclearentry()")+
calcbutton("+/-", "calcneg()")+
calcbutton("%", "calcpercent()")
)+
calctr(
calcbutton("7", "calcnumpressed(7)")+
calcbutton("8", "calcnumpressed(8)")+
calcbutton("9", "calcnumpressed(9)")+
calcbutton("/", "calcoperation('/')")
)+
calctr(
calcbutton("4", "calcnumpressed(4)")+
calcbutton("5", "calcnumpressed(5)")+
calcbutton("6", "calcnumpressed(6)")+
calcbutton("*", "calcoperation('*')")
)+
calctr(
calcbutton("1", "calcnumpressed(1)")+
calcbutton("2", "calcnumpressed(2)")+
calcbutton("3", "calcnumpressed(3)")+
calcbutton("-", "calcoperation('-')")
)+
calctr(
calcbutton("0", "calcnumpressed(0)")+
calcbutton(".", "calcdecimal()")+
calcbutton("=", "calcoperation('=')")+
calcbutton("+", "calcoperation('+')")
)));
}
// Insert calculator
//document.write(calc());
// Calculator functions
// Module-level variables
var calcprev=0; // calcprevious number (operand) awaiting operation
var calcfnew=false; // Flag to indicate a new number (operand) is being entered
var calcop=""; // Pending operation waiting for completion of second operand
// Set display
function calcsetdisplay(d) {
document.calckeypad.display.value=d;
document.calckeypad.display.align="center"; // NS bug fix
}
// Get display
function calcgetdisplay() {
return(document.calckeypad.display.value);
}
// Number button pressed
function calcnumpressed(num) {
if (calcfnew) {
calcsetdisplay(num);
calcfnew=false;
} else {
if (calcgetdisplay()=="0") {
calcsetdisplay(num);
} else {
calcsetdisplay(calcgetdisplay()+num);
}
}
}
// An operation key pressed
function calcoperation(key){
if (calcfnew && calcop!="="){
// User is hitting op keys repeatedly, so don't do anything
} else {
calcfnew=true;
if ('+'==calcop){
calcprev+=parseFloat(calcgetdisplay());
} else if ('-'==calcop){
calcprev-=parseFloat(calcgetdisplay());
} else if ('/'==calcop){
calcprev/=parseFloat(calcgetdisplay());
} else if ('*'==calcop){
calcprev*=parseFloat(calcgetdisplay());
} else {
calcprev=parseFloat(calcgetdisplay());
}
calcsetdisplay(calcprev);
calcop=key;
}
}
// Decimal point button pressed
function calcdecimal() {
if (calcfnew) {
calcsetdisplay("0.");
calcfnew=false;
} else {
if (calcgetdisplay().indexOf(".")==-1) {
calcsetdisplay(calcgetdisplay()+".");
}
}
}
// CE button pressed
function calcclearentry() {
// Remove current number and reset state
calcsetdisplay("0");
calcfnew=true;
}
// C button pressed
function calcclear() {
// Clear previous operand and pending operation, and clear display
calcprev=0;
calcop="";
calcclearentry();
}
// +/- button pressed
function calcneg() {
calcsetdisplay(-parseFloat(calcgetdisplay()));
}
// % button pressed
function calcpercent() {
calcsetdisplay(parseFloat(calcgetdisplay())/100*parseFloat(calcprev));
}
Voilà.
Source :
http://www.propix.hu/www/Calculator/Calculator.html
@ pluche.