JavaScript Calculator
Mem  
 
 
Instructions:
1: Copy and paste the following style declaration in the HEAD section of your page:
Select all...
<style type="text/css"> .bttnop { width:50px; color:darkred; } .bttn { width:50px; color:navy; } .answer { text-align:right; } </style>
2: Copy and paste the following javascript in the HEAD section of your page:
Select all...
<script language="javascript"> var num1=''; var num2=''; var num1set=false; var operation=''; var inmem=''; var w3c=(document.getElementById)?true:false; var ns4=(document.layers)?true:false; var ns6=(w3c && navigator.appName.indexOf("Netscape")>=0)?true:false; var whichkey; function setnum(num){ if((document.f.answer.value!='')&&num1set)document.f.answer.value=''; if(num1set){ num2+=num; document.f.answer.value=num2; }else{ num1+=num; document.f.answer.value=num1; }} function docalc(optionaloper){ if(optionaloper=='')num2=document.f.answer.value; if((num1!='')&&(num2!='')&&(operation!='')){ document.f.answer.value=eval(num1+operation+num2); if(optionaloper==''){ num1=''; num2=''; num1set=false; }else{ num1=document.f.answer.value; num1set=true; num2=''; } operation=optionaloper; }} function setoper(oper){ if(num1set&&(document.f.answer.value!='')&&(num2==''))operation=oper; if(num1set&&(operation!='')&&(num2!=''))docalc(oper); if(!num1set){ operation=oper; num1=document.f.answer.value; num1set=true; }} function backsp(){ if(document.f.answer.value.length>=1){ document.f.answer.value=document.f.answer.value.substring(0,document.f.answer.value.length-1); if(num1set) num2=document.f.answer.value; else num1=document.f.answer.value; }} function testdot(){ if(num1set) if(num2.indexOf('.')==(-1))setnum('.'); else if(num1.indexOf('.')==(-1))setnum('.'); } function memory(b){ switch(b){ case 0: inmem=''; document.f.box.checked=false; break; case 1:if(document.f.box.checked==true)document.f.answer.value=inmem; break; case 2: inmem=parseFloat(document.f.answer.value); document.f.box.checked=true; break; case 3: if(inmem==''){ inmem=document.f.answer.value; document.f.box.checked=true; }else inmem+=parseFloat(document.f.answer.value); break; }} function clearall(yes){ if(yes){ num1=''; num2=''; num1set=false; operation=''; }else num2=''; document.f.answer.value=num1; } function negate(){ if(num1set){ if(num2!=''){ num2=-num2; document.f.answer.value=num2; }}else{ if(num1!=''){ num1=-num1; document.f.answer.value=num1; }}} function percent(){ if((num1!='')&&(num2!='')){ num2=''+((parseFloat(num2))/100*parseFloat(num1)); document.f.answer.value=num2; }} function squareroot(){ if(num1set){ if(num2!=''){ num2=''+Math.sqrt(parseFloat(num2)); document.f.answer.value=num2; }}else{ if(num1!=''){ num1=''+Math.sqrt(parseFloat(num1)); document.f.answer.value=num1; }}} function invert(){ if(num1set){ if(num2!=''){ num2=''+1/parseFloat(num2); document.f.answer.value=num2; }}else{ if(num1!=''){ num1=''+1/parseFloat(num1); document.f.answer.value=num1; }}} function checkbx(){ (inmem!='')? document.f.box.checked=true: document.f.box.checked=false; alert('Do not click here.\n\nThis is to show if you have something in memory.'); document.f.box.blur(); } function showkeypress(keypress){ if(!(ns4||ns6))whichkey=event.keyCode; else whichkey=keypress.which; if((whichkey>=48)&&(whichkey<=57)){ whichkey=whichkey-48; setnum(whichkey.toString()); }else{ switch(whichkey){ case 27: clearall(true); break; case 13: docalc(''); break; case 42: setoper('*'); break; case 45: setoper('-'); break; case 43: setoper('+'); break; case 47: setoper('/'); break; case 46: testdot(); break; }}} if(ns4){ window.captureEvents(Event.KEYPRESS); window.onkeypress=showkeypress; }else document.onkeypress=showkeypress; </script>
3: Copy and paste the following HTML in the BODY section of your page:
Select all...
JavaScript Calculator
Mem