JS Calculator code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>javascript simple calculator </title> <script type=" Text/javascript "> <!--Hide The Script from old browsers--function compute (OBJ)//Click functions that are called by the ' = ' button; {obj.expr.value = eval (obj.expr.value)}//Use the JS eval () method to compute the formula in name expr var one = ' 1 ' var two = ' 2 ' var three = ' 3 ' var four = ' 4 ' var five = ' 5 ' var six = ' 6 ' var seven = ' 7 ' var eight = ' 8 ' var nine = ' 9 ' var zero = ' 0 ' var plus = ' + ' var minus = '-' var multiply = ' * ' var divide = '/' var decimal = '. function Enter (obj,string)//type= call to "button" {obj.expr.value + = string}//The corresponding character is appended to the value of the form expr//--end hiding here--&G T </script> </pead> <body> <form name= "Calc" > <table border= "1" > <TD colsPan= "4" ><input type= "text" name= "expr" size= "" action= "compute (this.form)"/></td> <tr> <TD ><input type= "button" value= "7" onclick= "Enter (This.form,seven)"/></td> <td><input-type= " Button "Value=" 8 "onclick=" Enter (this.form,eight) "/></td> <td><input type=" button "value=" 9 "oncli ck= "Enter (this.form,nine)"/></td> <td><input type= "button" value= "/" onclick= "Enter (This.form, Divide) "/></td> </tr> <tr> <td><input type=" button "value=" 4 "onclick=" Enter (this.form , four) "/></td> <td><input type= button" value= "5" onclick= "Enter (this.form,five)"/></td > <td><input type= "button" value= "6" onclick= "Enter (this.form,six)"/></td> <td><input Type= "button" value= "*" onclick= "Enter (this.form,multiply)"/></td> </tr> <tr> <td><in Put type= "button" value= "1" onclick= "enter" (tHis.form,one) "/></td> <td><input type= button" value= "2" onclick= "Enter (this.form,two)"/>< /td> <td><input type= "button" value= "3" onclick= "Enter (this.form,three)"/></td> <td>< Input type= "button" value= "-" onclick= "Enter (This.form,minus)"/></td> </tr> <tr> <td colspan = "2" ><input type= "button" value= "0" onclick= "Enter (This.form,zero)"/></td> <td><input type= "Button" value= ". "Onclick=" Enter (this.form,decimal) "/></td> <td><input type=" button "value=" + "onclick=" Enter ( This.form,plus) "/></td> </tr> <tr> <td colspan=" 2 "><input type=" button "value=" = "Oncl ick= "Compute (this.form)"/></td> <td colspan= "2" ><input type= "button" value= "AC" onclick= " Form.reset () "/></td> </tr> </table> </form> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Description
JavaScript eval () function
Definitions and usage
The eval () function computes a string and executes the JavaScript code in it.
return value
The value, if any, obtained by calculating the string.
Description
The method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.
If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.
Thrown
Throws an SyntaxError exception if there are no valid expressions and statements in the argument.
If Eval () is invoked illegally, a Evalerror exception is thrown.
If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.
Tips and comments
Tip: Although the eval () is very powerful, it is not much used in practice.