LoginSignup
3
3

More than 5 years have passed since last update.

HTMLにシンプルなJavaScript編集・実行環境を埋め込むスニペット

Last updated at Posted at 2013-01-11
jsrunner.html
<div id="jsrunner">
<textarea id="src" rows="8" style="width:100%;" onkeydown="
    var evt = event;
    if(evt.keyCode === 9){
        evt.preventDefault();
        var elm = evt.target;
        var val = elm.value;
        var pos = elm.selectionStart;
        elm.value = val.substr(0, pos) + '\t' + val.substr(pos, val.length);
        elm.setSelectionRange(pos + 1, pos + 1);
    }
">
now = new Date();
for(var i = 0; i < 200000000; i++){}
p(new Date() - now + 'ms');

/*
var name = window.prompt("hi, what's your name?");
p("hello, " + name + ". nice to meet you!");

var old = window.prompt("and, how old are you?") > 20 ? "adult":"young";
p("you are " + old);

undefined_variable;
*/
</textarea>
<div style="margin:1em 0;">
<input type="submit" value="Run" onclick="
    document.getElementById('stdout').innerHTML = '';
    document.getElementById('stderr').innerHTML = '';
    try {
        var p = 'function p(s){'
              + ' document.getElementById(\'stdout\').innerHTML += s+\'<br>\''
              + '}'
              + '\n';
        new Function(p + document.getElementById('src').value)();
    }catch(e){
        document.getElementById('stderr').innerHTML = e;
    }
">
<input type="submit" value="Clear" onclick="
    document.getElementById('stdout').innerHTML = '';
    document.getElementById('stderr').innerHTML = '';
">
</div>
<div id="stdout" style="border:1px solid silver;background:#f5f5f5;padding:0.5em;"></div>
<div id="stderr" style="color:red;padding:0.5em;"></div>
</div>
補足

テストコードはMac mini(mid 2011)のSafari6にて200000000回のループで計測。1000ms程度で実行完了。

3
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3