The Perfect JS Calculator code

Source: Internet
Author: User
The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<!--$Id: calculator.htm 14216 2008-03-10 02:27:21z Testyang $-->
<title> Calculator </title>
<script type= "Text/javascript" >
/* $Id: utils.js 5052 2007-02-03 10:30:13z Weberliu $ * *

var Browser = new Object ();

Browser.ismozilla = (typeof document.implementation!= ' undefined ') && (typeof Document.implementation.createDocument!= ' undefined ') && (typeof htmldocument!= ' undefined ');
Browser.isie = window. ActiveXObject? True:false;
Browser.isfirefox = (Navigator.userAgent.toLowerCase (). IndexOf ("Firefox")!=-1);
Browser.issafari = (Navigator.userAgent.toLowerCase (). IndexOf ("Safari")!=-1);
Browser.isopera = (Navigator.userAgent.toLowerCase (). IndexOf ("opera")!=-1);

var Utils = new Object ();

Utils.htmlencode = function (text)
{
Return Text.replace (/&/g, ' & '). Replace (/"/g, '"). Replace (/</g, ' < '). Replace (/>/g, ' > ');
}

Utils.trim = function (text)
{
  if (typeof (Text) = "string")
  {
    return Text.replace (/^s*|s*$/g, "");
 }
  Else
  {
    return text;
 }
}

Utils.isempty = function (val)
{
Switch (typeof (Val))
{
Case ' string ':
Return Utils.trim (val). length = 0? True:false;
Break
Case ' number ':
return val = = 0;
Break
Case ' object ':
return val = = null;
Break
Case ' array ':
return val.length = = 0;
Break
Default
return true;
}
}

Utils.isnumber = function (val)
{
var reg =/^[d|.|,]+$/;
Return Reg.test (Val);
}

Utils.isint = function (val)
{
if (val = = "")
{
return false;
}
var reg =/d+/;
Return!reg.test (Val);
}

Utils.isemail = function (email)
{
var reg1 =/([w.] +) @ ([[0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}.) | (([w-]+.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (]?) /;

return reg1.test (email);
}

Utils.istel = function (tel)
{
var reg =/^[d|-|s|_]+$/; Only use numbers-spaces, etc.

Return Reg.test (tel);
}

Utils.fixevent = function (e)
{
var evt = (typeof e = "undefined")? Window.event:e;
return evt;
}

Utils.srcelement = function (e)
{
if (typeof e = = "undefined") e = window.event;
var src = document.all? E.srcelement:e.target;

return SRC;
}

Utils.istime = function (val)
{
var reg =/^d{4}-d{2}-d{2}sd{2}:d {2}$/;

Return Reg.test (Val);
}

Utils.x = function (e)
{//current mouse x coordinates
return browser.isie?event.x + Document.documentelement.scrollleft-2:e.pagex;
}

Utils.y = function (e)
{//current mouse y-coordinate
return browser.isie?event.y + document.documentelement.scrolltop-2:e.pagey;
}

utils.request = function (URL, item)
{
var svalue=url.match (New RegExp ("[?]" "+item+" = ([^&]*) (ampersand), "I"));
return svalue?svalue[1]:svalue;
}

utils.$ = function (name)
{
return document.getElementById (name);
}

Function rowindex (tr)
{
  if (Browser.isie)
  {
    return tr.rowindex;
& nbsp
  Else
  {
    table = Tr.parentNode.parentNode
    for (i = 0; i < table.rows.length; i + +)
    {
      if (table.rows[i] = = TR)
       {
        return i;
     }
    }
 }

Document.getcookie = function (sname)
{
Cookies are separated by semicolons
var acookie = Document.cookie.split (";");
for (Var i=0 i < acookie.length; i++)
{
A Name/value pair (a crumb) is separated by a equal sign
var acrumb = acookie[i].split ("=");
if (sname = = Acrumb[0])
Return decodeURIComponent (acrumb[1]);
}

A cookie with the requested name does not exist
return null;
}

Document.setcookie = function (sname, svalue, Sexpires)
{
var SCookie = sname + "=" + encodeURIComponent (svalue);
if (sexpires!= null)
{
SCookie + = "; expires= "+ sexpires;
}

Document.cookie = SCookie;
}

Document.removecookie = function (Sname,svalue)
{
Document.cookie = sname + "=; Expires=fri, Dec 1999 23:59:59 GMT; ";
}

function GetPosition (o)
{
var t = o.offsettop;
var L = o.offsetleft;
while (o = o.offsetparent)
{
T + + o.offsettop;
L + + O.offsetleft;
}
var pos = {Top:t,left:l};
return POS;
}

function Cleanwhitespace (Element)
{
var element = element;
for (var i = 0; i < element.childNodes.length; i++) {
var node = element.childnodes[i];
if (Node.nodetype = = 3 &&!/s/.test (node.nodevalue))
Element.removechild (node);
}
}
</script>
<style type= "Text/css" >
Body, Div, input {
font:12px Arial;
}

. Calculatorbutton {
Text-align:center;
width:73px;
}

. calculatorButton2 {
Text-align:center;
width:154px;
}

*+html. CalculatorButton2 {
width:157px;
}

#calculator. Buttonarea {
padding:3px;
Border-color: #455690 #a6b4cf #a6b4cf #455690;
Border-style:solid;
border-width:1px;
}

#calculatorOutput {
padding:2px; BORDER:2PX inset; margin:2px;
}

#topbar {


Position:absolute;


Border-right: #455690 1px solid;


Border-top: #a6b4cf 1px solid;


Border-left: #a6b4cf 1px solid;


Border-bottom: #455690 1px solid;


Background-color: #c9d3f3;


width:300px;


Visibility:hidden;


z-index:99999;


Filter:progid:DXImageTransform.Microsoft.BasicImage (opacity=.65);


opacity:0.65;


}


&lt;/style&gt;


&lt;script type= "Text/javascript" &gt;


&lt;!--


var Calculator = Object ();

Calculator.result = 0;
Calculator.current = ';
calculator.values = 0;
Calculator.handle = ';

Calculator.elem = function () {return document.getElementById (' Calculator ');
Calculator.output = function () {return document.getElementById (' Calculatoroutput ');}

Calculator.input = function (n)
{
if (n = = "." && Calculator.current.toString (). IndexOf ('. ') >=0)
{
Return
}
var output = Calculator.output ();

if (calculator.current = = "0" && N!= ".") Calculator.current = ';

Calculator.current + = "" + N;

output.innerhtml = calculator.current;
}

Calculator.backspace = function ()
{
var output = Calculator.output ();
output.innerhtml = output.innerHTML.length > 1? Output.innerHTML.substr (0, output.innerhtml.length-1): 0;

Calculator.current = output.innerhtml;
}

Calculator.clear = function ()
{
Calculator.result = 0;
Calculator.current = ';
calculator.values = 0;
Calculator.handle = ';

var output = Calculator.output ();
output.innerhtml = "0";
}

calculator.calculate = function (p)


{


if (calculator.handle!= ' &amp;&amp; calculator.values!= ' &amp;&amp; calculator.current!= ')


{


Try


{


var value = eval (calculator.values + calculator.handle + calculator.current)


Calculator.values = value = = ' Infinity '? 0:value;


Calculator.output (). InnerHTML = Calculator.values


}


catch (E)


{


Alert (e);


}


}


Else


{


Calculator.values = calculator.current== '? Calculator.values:Calculator.current;


}

if (p = = ' = ')
{
Calculator.output (). InnerHTML = Calculator.values = = "? ' 0 ': calculator.values;
Calculator.current = calculator.values;
Calculator.handle = ';
}
Else
{
Calculator.handle = p;
}

Calculator.current = ';
}

onload = function () {
Window.focus ();
}
-->
</script>

&lt;body style= "Background:buttonface" &gt;


&lt;div class= "Buttonarea" &gt;


&lt;div id= "Calculatoroutput" style= "width:95%; text-align:right;border:2px inset;background: #FFF; " &gt;0&lt;/div&gt;


&lt;table width= "100%" &gt;


&lt;tr&gt;


&LT;TD colspan= "2" &gt;&lt;input type= "button" class= "CalculatorButton2" value= "Early Love" onclick= "Calculator.clear" () &gt;&lt;/td&gt;


&LT;TD colspan= "2" &gt;&lt;input type= "button" class= "CalculatorButton2" value= "backspace" onclick= "Calculator.backspace" () /&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;&lt;input class= "Calculatorbutton" type= "button" value= "7" onclick= "Calculator.input (7)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "8" class= "Calculatorbutton" onclick= "Calculator.input (8)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "9" class= "Calculatorbutton" onclick= "Calculator.input (9)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "/" class= "Calculatorbutton" onclick= "calculator.calculate ('/')"/&gt;&lt;/ Td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;&lt;input type= "button" value= "4" class= "Calculatorbutton" onclick= "Calculator.input (4)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "5" class= "Calculatorbutton" onclick= "Calculator.input (5)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "6" class= "Calculatorbutton" onclick= "Calculator.input (6)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "*" class= "Calculatorbutton" onclick= "calculator.calculate (' *")/&gt;&lt;/ Td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;&lt;input type= "button" value= "1" class= "Calculatorbutton" onclick= "calculator.input (1)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "2" class= "Calculatorbutton" onclick= "Calculator.input (2)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "3" class= "Calculatorbutton" onclick= "Calculator.input (3)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "-" class= "Calculatorbutton" onclick= "calculator.calculate ('-')"/&gt;&lt;/ Td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;&lt;input type= "button" value= "0" class= "Calculatorbutton" onclick= "calculator.input (0)"/&gt;&lt;/td&gt;


&lt;td&gt;&lt;input type= "button" value= "" class= "Calculatorbutton" onclick= "calculator.input ('. ')"/&gt;&lt;/td &gt;


&lt;td&gt;&lt;input type= "button" value= "=" class= "Calculatorbutton" onclick= "calculator.calculate (' = ')"/&gt;&lt;/ Td&gt;


&lt;td&gt;&lt;input type= "button" value= "+" class= "Calculatorbutton" onclick= "calculator.calculate (' + ')"/&gt;&lt;/ Td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD height= &gt; &lt;/td&gt;


&LT;TD colspan= "2" &gt;&lt;div align= "center" &gt;&lt;a href= "#" onclick= "Top.close ()" &gt; Close window &lt;/a&gt;&lt;/div&gt; &lt;/td&gt;


&lt;td&gt; &lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;


&lt;/div&gt;


&lt;/body&gt;

<script type= "Text/javascript" >
<!--

Document.onkeyup = function (e)
{
var evt = utils.fixevent (e);

if (Evt.keycode &gt;= &amp;&amp; evt.keycode &lt;= &amp;&amp;!evt.shiftkey) | |


(Evt.keycode &gt;= &amp;&amp; evt.keycode &lt;=!evt.shiftkey))


{


if (Evt.keycode &gt; 57)


{


Calculator.input (evt.keycode-96);


}


Else


{


Calculator.input (evt.keycode-48);


}


}


else if ((Evt.keycode = = &amp;&amp;!evt.shiftkey) | | (Evt.keycode = &amp;&amp; Evt.shiftkey) | | (Evt.keycode = = 187 &amp;&amp; evt.shiftkey))


{


Calculator.calculate (' + ');


}


else if ((Evt.keycode = = 109 &amp;&amp;!evt.shiftkey) | | (Evt.keycode = = 189 &amp;&amp;!evt.shiftkey))


{


Calculator.calculate ('-');


}


else if ((Evt.keycode = &amp;&amp;!evt.shiftkey) | | (Evt.keycode = &amp;&amp; evt.shiftkey))


{


Calculator.calculate (' * ');


}


else if ((Evt.keycode = &amp;&amp;!evt.shiftkey) | | (Evt.keycode = = 191 &amp;&amp;!evt.shiftkey))


{


Calculator.calculate ('/');


}


else if (Evt.keycode = 13 | | (Evt.keycode = &amp;&amp;!evt.shiftkey) | | (Evt.keycode = = 187 &amp;&amp;!evt.shiftkey))


{


Calculator.calculate (' = ');


}


else if ((Evt.keycode = &amp;&amp;!evt.shiftkey) | | (Evt.keycode = = 190 &amp;&amp;!evt.shiftkey))


{


Calculator.input ('. ');


}


else if (Evt.keycode = 27)


{


Calculator.clear ();


}


else if (Evt.keycode = 8)


{


Calculator.backspace ();


}

return false;

alert (Evt.keycode);
}
-->
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.