-
Notifications
You must be signed in to change notification settings - Fork 0
/
45-js-bench.html
37 lines (32 loc) · 979 Bytes
/
45-js-bench.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas</title>
<link rel="stylesheet" href="./lib/game.css" />
</head>
<textarea id="code">return "hi";</textarea>
<button id="button">Run</button>
<pre id="output"></pre>
<body>
<script src="./lib/coffeescript.js"></script>
<script type="text/coffeescript">
###
Build an interface that allows people to type and run pieces of JavaScript code.
Put a button next to a <textarea> field, which, when pressed, uses the Function
constructor we saw in Chapter 10 to wrap the text in a function and call it.
Convert the return value of the function, or any error it raised, to a string
and display it after the text field.
###
code = document.querySelector '#code'
button = document.querySelector '#button'
output = document.querySelector '#output'
button.addEventListener 'click', () ->
try
fn = new Function(code.value)
output.innerHTML = fn()
catch err
output.innerHTML = err
</script>
</body>
</html>