Run JavaScript live on your Website

I’ve just written coderunner.js. It allows you to write standard <pre> tags and then convert them into executable code that your users can run on their computer. Here is an example:

for (var i = 0; i < 10; i++) {
  console.log('Number ' + i);
}

You can start coderunner.js by including the file itself, the stylesheet and then initializing like this:

document.addEventListener('DOMContentLoaded', function() {
  var autoRunner = new CodeRunner({ autorun: true });
  autoRunner.makeExecutable('pre.executable.autorun');

  var runner = new CodeRunner({ autorun: false });
  runner.makeExecutable('pre.executable:not(.autorun)');
});

coderunner.js has no dependencies and is about 2 KB in size (uncompressed). It is currently only tested in WebKit and has no issues with prettify by Google.

Leave a Reply

Your email address will not be published. Required fields are marked *