<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd ">
<meta http-equiv = "Content-type" content= "Text/html;charset=utf-8"/>
<script src= "Js.js" type= "Text/javascript" language= "JavaScript" >
</script>
<body>
<form>
<input type= "button" value= "1" onclick = "cal (This.value);" /> <!--calling Functions--
<input type= "button" value= "2" onclick = "cal (This.value);" />
<input type= "button" value= "3" onclick = "cal (This.value);" />
<input type= "button" value= "4" onclick = "cal (This.value);" />
<input type= "button" value= "+" onclick = "cal (This.value);" />
<input type= "button" value= "-" onclick = "cal (This.value);" />
<input type= "button" value= "*" onclick = "cal (This.value);" />
<input type= "button" value= "/" onclick = "cal (This.value);" />
<input type= "button" value= "=" onclick = "cal (This.value);" /><br/>
<input type = "text" id = "text" name = "Text"/>
</form>
</body>
Js.js
Function cal (value) {Var text = document.getelementbyid ("text"). value;var last = text.charat (text.length-1);// gets the last element in the text box if (text == null) {text = ""; }else{if ((value == "+") | | (value == "-") | | (value == "*") | | (value == "/")) {//input is + - / *if ((last == "+") | | (last == "-") | | (last == "*") | | (last == "/")) The last element in the {///text box is + - * / Error alert when input + - / * ("incorrect input");} else{//the last element in the text box is + - * / re-enters the number when it is added to the back Text += value;}} Else if (value == "=") {//input is = if ((last == "+") | | (last == "-") | | (last == "*") | | (last == "/")) {//Enter = , when the last element is + - * / , cannot calculate only the number of the last symbol is displayed text = Text.substring (0,text.length-1), }else{//input is = the last one is the number can be calculated text = eval (text); /// Use eval (str) Calculate }}else{//input is digital direct display Text += value;}} document.getElementById ("text"). value = text; //returns the final result to the text box}