|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>Calculator</title> |
| 6 | + <style> |
| 7 | + html { |
| 8 | + color: #000; |
| 9 | + background-color: #fff; |
| 10 | + font-family: Arial, sans-serif; |
| 11 | + font-size: 2em; |
| 12 | + line-height: 1.1; |
| 13 | + } |
| 14 | + |
| 15 | + h1 { |
| 16 | + font-size: 2em; |
| 17 | + font-weight: normal; |
| 18 | + margin-top:10px; |
| 19 | + margin-bottom:20px; |
| 20 | + } |
| 21 | + |
| 22 | + input,select,button { |
| 23 | + text-align:right; |
| 24 | + margin-bottom:5px; |
| 25 | + font-size: 0.7em; |
| 26 | + } |
| 27 | + |
| 28 | + #content { |
| 29 | + width:400px; |
| 30 | + margin:0px auto; |
| 31 | + text-align:center; |
| 32 | + } |
| 33 | + |
| 34 | + #calculator { |
| 35 | + width:200px; |
| 36 | + margin:0px auto; |
| 37 | + text-align: right; |
| 38 | + margin-bottom: 10px; |
| 39 | + } |
| 40 | + |
| 41 | + .error { |
| 42 | + color: #dd0000; |
| 43 | + } |
| 44 | + |
| 45 | + img { |
| 46 | + display:block; |
| 47 | + margin-left: auto; |
| 48 | + margin-right: auto; |
| 49 | + } |
| 50 | + </style> |
| 51 | +</head> |
| 52 | +<body> |
| 53 | + <div id="content"> |
| 54 | + <img src="https://s3-us-west-2.amazonaws.com/simple-calculator-website-demo/calculator.png"/> |
| 55 | + <h1>Simple Calculator Service</h1> |
| 56 | + <div id="calculator"> |
| 57 | + <input id="a" type="text" name="a" size="8" /><br/> |
| 58 | + <select id="operation" name="operation"> |
| 59 | + <option value="add" selected>+</option> |
| 60 | + <option value="subtract">-</option> |
| 61 | + <option value="multiply">*</option> |
| 62 | + <option value="divide">/</option> |
| 63 | + </select><br/> |
| 64 | + <input id="b" type="text" name="b" size="8" /><br/> |
| 65 | + <button id="calculate" name="calculate">Calculate</button><br/> |
| 66 | + </div> |
| 67 | + <span id="result"></span> |
| 68 | + </div> |
| 69 | + <script> |
| 70 | + var aInput = document.getElementById("a"); |
| 71 | + var bInput = document.getElementById("b"); |
| 72 | + var operationSelect = document.getElementById("operation"); |
| 73 | + var calculateButton = document.getElementById("calculate"); |
| 74 | + var resultInput = document.getElementById("result"); |
| 75 | + calculateButton.addEventListener("click", function() { |
| 76 | + var operation = operationSelect.options[operationSelect.selectedIndex].value; |
| 77 | + var a = aInput.value; |
| 78 | + var b = bInput.value; |
| 79 | + var xhr = new XMLHttpRequest(); |
| 80 | + xhr.open('GET', encodeURI('/' + operation + '?a=' + a + '&b=' + b)); |
| 81 | + xhr.onload = function() { |
| 82 | + if (xhr.status === 200) { |
| 83 | + result.innerHTML = xhr.responseText; |
| 84 | + } |
| 85 | + else { |
| 86 | + result.innerHTML = '<span class="error">' + xhr.responseText + '<span>'; |
| 87 | + } |
| 88 | + }; |
| 89 | + xhr.send(); |
| 90 | + }); |
| 91 | + </script> |
| 92 | +</body> |
| 93 | +</html> |
0 commit comments